const db = require('../models');
const Users = db.users;

const Organizations = db.organizations;

const Brands = db.brands;

const BrandRateCards = db.brand_rate_cards;

const ClientOrders = db.client_orders;

const Countries = db.countries;

const Clients = db.clients;

const DefaultActivitiesProducts = db.default_activities_products;

const Departments = db.departments;

const Markets = db.markets;

const OutworkLineItems = db.outwork_line_items;

const Reports = db.reports;

const SavedAdaptations = db.saved_adaptations;

const SpecsDatabase = db.specs_database;

const Suppliers = db.suppliers;

const SupportingDocuments = db.supporting_documents;

const TrackingCategories = db.tracking_categories;

const ZipflowWorkflows = db.zipflow_workflows;

const Quotes = db.quotes;

const AdbuilderAssets = db.adbuilder_assets;

const ClientDam = db.client_dam;

const Outwork = db.outwork;

const AdbuilderTemplates = db.adbuilder_templates;

const Campaigns = db.campaigns;

const OrganizationsData = [
  {
    name: 'John Bardeen',
  },

  {
    name: 'Louis Victor de Broglie',
  },

  {
    name: 'Theodosius Dobzhansky',
  },

  {
    name: 'Hans Bethe',
  },

  {
    name: 'Arthur Eddington',
  },
];

const BrandsData = [
  {
    // type code here for "relation_one" field

    name: 'Louis Pasteur',

    rate_card_import: true,

    client_fixed_text: 'Michael Faraday',

    yes_fixed_text: 'John Dalton',

    default_client_contact: 'Heike Kamerlingh Onnes',

    // type code here for "relation_one" field

    // type code here for "relation_one" field

    inherit_default_rate_card: true,

    // type code here for "relation_one" field

    default_costs: 32.93,

    production_fee: 90.91,

    insurance_fee: 87.48,

    temp: 'Johannes Kepler',

    xero_id: 'Ernst Haeckel',
  },

  {
    // type code here for "relation_one" field

    name: 'James Clerk Maxwell',

    rate_card_import: true,

    client_fixed_text: 'Francis Galton',

    yes_fixed_text: 'Archimedes',

    default_client_contact: 'Hans Selye',

    // type code here for "relation_one" field

    // type code here for "relation_one" field

    inherit_default_rate_card: false,

    // type code here for "relation_one" field

    default_costs: 41.66,

    production_fee: 40.33,

    insurance_fee: 88.85,

    temp: 'Edward Teller',

    xero_id: 'Gustav Kirchhoff',
  },

  {
    // type code here for "relation_one" field

    name: 'Sheldon Glashow',

    rate_card_import: true,

    client_fixed_text: 'Louis Victor de Broglie',

    yes_fixed_text: 'Werner Heisenberg',

    default_client_contact: 'Hans Bethe',

    // type code here for "relation_one" field

    // type code here for "relation_one" field

    inherit_default_rate_card: false,

    // type code here for "relation_one" field

    default_costs: 41.08,

    production_fee: 28.64,

    insurance_fee: 53.57,

    temp: 'Lucretius',

    xero_id: 'Hans Bethe',
  },

  {
    // type code here for "relation_one" field

    name: 'James Clerk Maxwell',

    rate_card_import: true,

    client_fixed_text: 'Charles Lyell',

    yes_fixed_text: 'Erwin Schrodinger',

    default_client_contact: 'Max Born',

    // type code here for "relation_one" field

    // type code here for "relation_one" field

    inherit_default_rate_card: true,

    // type code here for "relation_one" field

    default_costs: 53.27,

    production_fee: 35.52,

    insurance_fee: 93.15,

    temp: 'Francis Galton',

    xero_id: 'John Bardeen',
  },

  {
    // type code here for "relation_one" field

    name: 'Emil Fischer',

    rate_card_import: false,

    client_fixed_text: 'Max von Laue',

    yes_fixed_text: 'Galileo Galilei',

    default_client_contact: 'Thomas Hunt Morgan',

    // type code here for "relation_one" field

    // type code here for "relation_one" field

    inherit_default_rate_card: true,

    // type code here for "relation_one" field

    default_costs: 41.19,

    production_fee: 42.45,

    insurance_fee: 89.52,

    temp: 'Jean Piaget',

    xero_id: 'Werner Heisenberg',
  },
];

const BrandRateCardsData = [
  {
    name: 'Justus Liebig',

    rate_card_type: 'Gertrude Belle Elion',

    // type code here for "relation_one" field

    // type code here for "relation_one" field

    status: 'Alfred Kinsey',

    currency: 'Louis Pasteur',

    push_new_values: false,

    inherit_default_rate_card: false,

    service_count: 9,

    discipline: 'Marcello Malpighi',

    output_channel: 'Albert Einstein',

    staff_skills: 'Sigmund Freud',

    rate_card_owner: 'Wilhelm Wundt',

    rate_card_id: 'Leonard Euler',

    markets: 'Emil Kraepelin',

    languages: 'William Bayliss',

    valid_from: new Date(Date.now()),

    valid_to: new Date(Date.now()),

    version_number: 'Max Planck',

    commercial_owner: 'Trofim Lysenko',

    review_date: new Date(Date.now()),

    assumptions: 'Leonard Euler',

    no_fixed_text: 'Albrecht von Haller',

    yes_fixed_text: 'Alfred Kinsey',
  },

  {
    name: 'Ernest Rutherford',

    rate_card_type: 'Enrico Fermi',

    // type code here for "relation_one" field

    // type code here for "relation_one" field

    status: 'John Dalton',

    currency: 'Claude Bernard',

    push_new_values: true,

    inherit_default_rate_card: true,

    service_count: 3,

    discipline: 'Alexander Fleming',

    output_channel: 'Charles Sherrington',

    staff_skills: 'Francis Crick',

    rate_card_owner: 'Alexander Fleming',

    rate_card_id: 'Karl Landsteiner',

    markets: 'Galileo Galilei',

    languages: 'Alexander Fleming',

    valid_from: new Date(Date.now()),

    valid_to: new Date(Date.now()),

    version_number: 'Louis Pasteur',

    commercial_owner: 'Euclid',

    review_date: new Date(Date.now()),

    assumptions: 'Jonas Salk',

    no_fixed_text: 'Edward O. Wilson',

    yes_fixed_text: 'Pierre Simon de Laplace',
  },

  {
    name: 'Leonard Euler',

    rate_card_type: 'Ernest Rutherford',

    // type code here for "relation_one" field

    // type code here for "relation_one" field

    status: 'James Clerk Maxwell',

    currency: 'Edwin Hubble',

    push_new_values: false,

    inherit_default_rate_card: true,

    service_count: 9,

    discipline: 'Hermann von Helmholtz',

    output_channel: 'Anton van Leeuwenhoek',

    staff_skills: 'Claude Bernard',

    rate_card_owner: 'Marcello Malpighi',

    rate_card_id: 'Alfred Kinsey',

    markets: 'Sheldon Glashow',

    languages: 'Trofim Lysenko',

    valid_from: new Date(Date.now()),

    valid_to: new Date(Date.now()),

    version_number: 'Lynn Margulis',

    commercial_owner: 'Enrico Fermi',

    review_date: new Date(Date.now()),

    assumptions: 'Sheldon Glashow',

    no_fixed_text: 'Louis Victor de Broglie',

    yes_fixed_text: 'Ernst Mayr',
  },

  {
    name: 'Konrad Lorenz',

    rate_card_type: 'Ernst Haeckel',

    // type code here for "relation_one" field

    // type code here for "relation_one" field

    status: 'Justus Liebig',

    currency: 'William Harvey',

    push_new_values: true,

    inherit_default_rate_card: false,

    service_count: 5,

    discipline: 'Gustav Kirchhoff',

    output_channel: 'Edward Teller',

    staff_skills: 'Sigmund Freud',

    rate_card_owner: 'Claude Levi-Strauss',

    rate_card_id: 'Paul Dirac',

    markets: 'Claude Bernard',

    languages: 'Marcello Malpighi',

    valid_from: new Date(Date.now()),

    valid_to: new Date(Date.now()),

    version_number: 'Rudolf Virchow',

    commercial_owner: 'Hans Selye',

    review_date: new Date(Date.now()),

    assumptions: 'Anton van Leeuwenhoek',

    no_fixed_text: 'Paul Ehrlich',

    yes_fixed_text: 'Joseph J. Thomson',
  },

  {
    name: 'Erwin Schrodinger',

    rate_card_type: 'Robert Koch',

    // type code here for "relation_one" field

    // type code here for "relation_one" field

    status: 'Francis Galton',

    currency: 'Franz Boas',

    push_new_values: false,

    inherit_default_rate_card: false,

    service_count: 6,

    discipline: 'Heike Kamerlingh Onnes',

    output_channel: 'Thomas Hunt Morgan',

    staff_skills: 'Carl Linnaeus',

    rate_card_owner: 'Carl Gauss (Karl Friedrich Gauss)',

    rate_card_id: 'Edward Teller',

    markets: 'Charles Darwin',

    languages: 'Lucretius',

    valid_from: new Date(Date.now()),

    valid_to: new Date(Date.now()),

    version_number: 'Theodosius Dobzhansky',

    commercial_owner: 'Karl Landsteiner',

    review_date: new Date(Date.now()),

    assumptions: 'William Harvey',

    no_fixed_text: 'Isaac Newton',

    yes_fixed_text: 'Karl Landsteiner',
  },
];

const ClientOrdersData = [
  {
    status: 'Alfred Wegener',

    order_name: 'William Herschel',

    order_number: 'George Gaylord Simpson',

    // type code here for "relation_many" field
  },

  {
    status: 'Joseph J. Thomson',

    order_name: 'Edwin Hubble',

    order_number: 'Carl Gauss (Karl Friedrich Gauss)',

    // type code here for "relation_many" field
  },

  {
    status: 'Richard Feynman',

    order_name: 'Antoine Laurent Lavoisier',

    order_number: 'Edward Teller',

    // type code here for "relation_many" field
  },

  {
    status: 'Sigmund Freud',

    order_name: 'Claude Levi-Strauss',

    order_number: 'Charles Sherrington',

    // type code here for "relation_many" field
  },

  {
    status: 'Edward Teller',

    order_name: 'Louis Pasteur',

    order_number: 'Linus Pauling',

    // type code here for "relation_many" field
  },
];

const CountriesData = [
  {
    name: 'Rudolf Virchow',

    currency: 'Erwin Schrodinger',

    default_prefill: 'Francis Crick',
  },

  {
    name: 'Lynn Margulis',

    currency: 'John Dalton',

    default_prefill: 'Louis Victor de Broglie',
  },

  {
    name: 'William Harvey',

    currency: 'Noam Chomsky',

    default_prefill: 'Enrico Fermi',
  },

  {
    name: 'Galileo Galilei',

    currency: 'Gustav Kirchhoff',

    default_prefill: 'Max Born',
  },

  {
    name: 'Joseph J. Thomson',

    currency: 'Charles Darwin',

    default_prefill: 'J. Robert Oppenheimer',
  },
];

const ClientsData = [
  {
    thyme_id: 'Werner Heisenberg',

    name: 'Antoine Laurent Lavoisier',

    office: 'Alfred Binet',

    address: 'Comte de Buffon',

    city: 'Charles Lyell',

    post_code: 'Michael Faraday',

    // type code here for "relation_one" field

    country_txt: 'Edwin Hubble',

    phone_number: 'Sigmund Freud',

    website: 'Arthur Eddington',

    currency: 'Trofim Lysenko',

    curious_customer_id: 'Heike Kamerlingh Onnes',

    // type code here for "relation_one" field

    agency: 'Claude Levi-Strauss',

    inclusive_amends: true,

    early_payment_discount: 45.96,

    client_id_link: 'Albert Einstein',

    public_url: 'Ludwig Boltzmann',

    // type code here for "relation_many" field

    production_fee: 48.96,

    insurance_fee: 99.35,

    xero_id: 'Isaac Newton',
  },

  {
    thyme_id: 'Carl Linnaeus',

    name: 'Sigmund Freud',

    office: 'Louis Victor de Broglie',

    address: 'Edwin Hubble',

    city: 'Comte de Buffon',

    post_code: 'John Dalton',

    // type code here for "relation_one" field

    country_txt: 'Andreas Vesalius',

    phone_number: 'Nicolaus Copernicus',

    website: 'Trofim Lysenko',

    currency: 'Noam Chomsky',

    curious_customer_id: 'Nicolaus Copernicus',

    // type code here for "relation_one" field

    agency: 'Heike Kamerlingh Onnes',

    inclusive_amends: true,

    early_payment_discount: 73.08,

    client_id_link: 'Theodosius Dobzhansky',

    public_url: 'Max Planck',

    // type code here for "relation_many" field

    production_fee: 84.09,

    insurance_fee: 56.48,

    xero_id: 'Max Planck',
  },

  {
    thyme_id: 'Gustav Kirchhoff',

    name: 'Lynn Margulis',

    office: 'Alexander Fleming',

    address: 'Gertrude Belle Elion',

    city: 'Edwin Hubble',

    post_code: 'Hermann von Helmholtz',

    // type code here for "relation_one" field

    country_txt: 'Nicolaus Copernicus',

    phone_number: 'Francis Galton',

    website: 'Lucretius',

    currency: 'Richard Feynman',

    curious_customer_id: 'Sigmund Freud',

    // type code here for "relation_one" field

    agency: 'Alfred Kinsey',

    inclusive_amends: false,

    early_payment_discount: 76.16,

    client_id_link: 'Ernst Haeckel',

    public_url: 'John Dalton',

    // type code here for "relation_many" field

    production_fee: 93.42,

    insurance_fee: 92.62,

    xero_id: 'Theodosius Dobzhansky',
  },

  {
    thyme_id: 'Alfred Wegener',

    name: 'Gregor Mendel',

    office: 'Louis Pasteur',

    address: 'Heike Kamerlingh Onnes',

    city: 'Isaac Newton',

    post_code: 'John Bardeen',

    // type code here for "relation_one" field

    country_txt: 'Marcello Malpighi',

    phone_number: 'Neils Bohr',

    website: 'John Dalton',

    currency: 'Andreas Vesalius',

    curious_customer_id: 'William Harvey',

    // type code here for "relation_one" field

    agency: 'James Watson',

    inclusive_amends: false,

    early_payment_discount: 34.82,

    client_id_link: 'Murray Gell-Mann',

    public_url: 'Tycho Brahe',

    // type code here for "relation_many" field

    production_fee: 56.44,

    insurance_fee: 71.47,

    xero_id: 'Wilhelm Wundt',
  },

  {
    thyme_id: 'Jean Baptiste Lamarck',

    name: 'Euclid',

    office: 'Claude Levi-Strauss',

    address: 'Richard Feynman',

    city: 'Michael Faraday',

    post_code: 'Max Delbruck',

    // type code here for "relation_one" field

    country_txt: 'Carl Linnaeus',

    phone_number: 'Stephen Hawking',

    website: 'Claude Levi-Strauss',

    currency: 'John Dalton',

    curious_customer_id: 'James Clerk Maxwell',

    // type code here for "relation_one" field

    agency: 'Ernest Rutherford',

    inclusive_amends: false,

    early_payment_discount: 62.47,

    client_id_link: 'Thomas Hunt Morgan',

    public_url: 'Charles Sherrington',

    // type code here for "relation_many" field

    production_fee: 92.05,

    insurance_fee: 67.27,

    xero_id: 'Noam Chomsky',
  },
];

const DefaultActivitiesProductsData = [
  {
    name: 'John Dalton',

    rate_type: 'Albrecht von Haller',

    standard_service: 'Murray Gell-Mann',

    // type code here for "relation_one" field

    service_text: 'Edward Teller',

    rate: 43.82,

    service_id: 'Albert Einstein',

    service_code: 'Karl Landsteiner',

    unit_type: 'Francis Crick',

    cost: 67.82,

    qty: 99.11,

    currency: 'John Dalton',

    rate_gbp: 72.41,

    rate_eur: 71.96,

    rate_usd: 42.09,

    yes_fixed_text: false,

    no_fixed_text: false,
  },

  {
    name: 'Jonas Salk',

    rate_type: 'Hermann von Helmholtz',

    standard_service: 'Charles Lyell',

    // type code here for "relation_one" field

    service_text: 'Ernest Rutherford',

    rate: 30.24,

    service_id: 'Hans Bethe',

    service_code: 'Carl Linnaeus',

    unit_type: 'Alfred Binet',

    cost: 77.78,

    qty: 52.81,

    currency: 'Emil Fischer',

    rate_gbp: 33.97,

    rate_eur: 71.22,

    rate_usd: 49.47,

    yes_fixed_text: true,

    no_fixed_text: false,
  },

  {
    name: 'Karl Landsteiner',

    rate_type: 'Carl Linnaeus',

    standard_service: 'Dmitri Mendeleev',

    // type code here for "relation_one" field

    service_text: 'Nicolaus Copernicus',

    rate: 79.47,

    service_id: 'Carl Gauss (Karl Friedrich Gauss)',

    service_code: 'Murray Gell-Mann',

    unit_type: 'Erwin Schrodinger',

    cost: 36.21,

    qty: 91.43,

    currency: 'Carl Gauss (Karl Friedrich Gauss)',

    rate_gbp: 95.83,

    rate_eur: 25.16,

    rate_usd: 72.45,

    yes_fixed_text: false,

    no_fixed_text: false,
  },

  {
    name: 'James Clerk Maxwell',

    rate_type: 'Edwin Hubble',

    standard_service: 'Gustav Kirchhoff',

    // type code here for "relation_one" field

    service_text: 'Hermann von Helmholtz',

    rate: 86.03,

    service_id: 'Isaac Newton',

    service_code: 'Sheldon Glashow',

    unit_type: 'Neils Bohr',

    cost: 42.33,

    qty: 37.93,

    currency: 'Claude Bernard',

    rate_gbp: 66.92,

    rate_eur: 25.69,

    rate_usd: 90.31,

    yes_fixed_text: false,

    no_fixed_text: false,
  },

  {
    name: 'George Gaylord Simpson',

    rate_type: 'Claude Levi-Strauss',

    standard_service: 'Charles Lyell',

    // type code here for "relation_one" field

    service_text: 'Alfred Wegener',

    rate: 88.23,

    service_id: 'Claude Bernard',

    service_code: 'Theodosius Dobzhansky',

    unit_type: 'Linus Pauling',

    cost: 99.69,

    qty: 54.82,

    currency: 'Ludwig Boltzmann',

    rate_gbp: 13.86,

    rate_eur: 27.97,

    rate_usd: 63.34,

    yes_fixed_text: true,

    no_fixed_text: false,
  },
];

const DepartmentsData = [
  {
    name: 'William Harvey',

    timing_id: 'Gertrude Belle Elion',
  },

  {
    name: 'Jonas Salk',

    timing_id: 'Trofim Lysenko',
  },

  {
    name: 'Alfred Wegener',

    timing_id: 'Willard Libby',
  },

  {
    name: 'Jonas Salk',

    timing_id: 'Francis Galton',
  },

  {
    name: 'Frederick Sanger',

    timing_id: 'Isaac Newton',
  },
];

const MarketsData = [
  {
    // type code here for "relation_one" field

    market: 'Jean Baptiste Lamarck',
  },

  {
    // type code here for "relation_one" field

    market: 'August Kekule',
  },

  {
    // type code here for "relation_one" field

    market: 'Linus Pauling',
  },

  {
    // type code here for "relation_one" field

    market: 'Max Delbruck',
  },

  {
    // type code here for "relation_one" field

    market: 'Richard Feynman',
  },
];

const OutworkLineItemsData = [{}, {}, {}, {}, {}];

const ReportsData = [{}, {}, {}, {}, {}];

const SavedAdaptationsData = [{}, {}, {}, {}, {}];

const SpecsDatabaseData = [{}, {}, {}, {}, {}];

const SuppliersData = [{}, {}, {}, {}, {}];

const SupportingDocumentsData = [{}, {}, {}, {}, {}];

const TrackingCategoriesData = [{}, {}, {}, {}, {}];

const ZipflowWorkflowsData = [{}, {}, {}, {}, {}];

const QuotesData = [{}, {}, {}, {}, {}];

const AdbuilderAssetsData = [{}, {}, {}, {}, {}];

const ClientDamData = [{}, {}, {}, {}, {}];

const OutworkData = [{}, {}, {}, {}, {}];

const AdbuilderTemplatesData = [{}, {}, {}, {}, {}];

const CampaignsData = [{}, {}, {}, {}, {}];

// Similar logic for "relation_many"

async function associateBrandWithClient() {
  const relatedClient0 = await Clients.findOne({
    offset: Math.floor(Math.random() * (await Clients.count())),
  });
  const Brand0 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Brand0?.setClient) {
    await Brand0.setClient(relatedClient0);
  }

  const relatedClient1 = await Clients.findOne({
    offset: Math.floor(Math.random() * (await Clients.count())),
  });
  const Brand1 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Brand1?.setClient) {
    await Brand1.setClient(relatedClient1);
  }

  const relatedClient2 = await Clients.findOne({
    offset: Math.floor(Math.random() * (await Clients.count())),
  });
  const Brand2 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Brand2?.setClient) {
    await Brand2.setClient(relatedClient2);
  }

  const relatedClient3 = await Clients.findOne({
    offset: Math.floor(Math.random() * (await Clients.count())),
  });
  const Brand3 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 3,
  });
  if (Brand3?.setClient) {
    await Brand3.setClient(relatedClient3);
  }

  const relatedClient4 = await Clients.findOne({
    offset: Math.floor(Math.random() * (await Clients.count())),
  });
  const Brand4 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 4,
  });
  if (Brand4?.setClient) {
    await Brand4.setClient(relatedClient4);
  }
}

async function associateBrandWithDefault_account_manager() {
  const relatedDefault_account_manager0 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const Brand0 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Brand0?.setDefault_account_manager) {
    await Brand0.setDefault_account_manager(relatedDefault_account_manager0);
  }

  const relatedDefault_account_manager1 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const Brand1 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Brand1?.setDefault_account_manager) {
    await Brand1.setDefault_account_manager(relatedDefault_account_manager1);
  }

  const relatedDefault_account_manager2 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const Brand2 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Brand2?.setDefault_account_manager) {
    await Brand2.setDefault_account_manager(relatedDefault_account_manager2);
  }

  const relatedDefault_account_manager3 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const Brand3 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 3,
  });
  if (Brand3?.setDefault_account_manager) {
    await Brand3.setDefault_account_manager(relatedDefault_account_manager3);
  }

  const relatedDefault_account_manager4 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const Brand4 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 4,
  });
  if (Brand4?.setDefault_account_manager) {
    await Brand4.setDefault_account_manager(relatedDefault_account_manager4);
  }
}

async function associateBrandWithDefault_sale() {
  const relatedDefault_sale0 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const Brand0 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Brand0?.setDefault_sale) {
    await Brand0.setDefault_sale(relatedDefault_sale0);
  }

  const relatedDefault_sale1 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const Brand1 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Brand1?.setDefault_sale) {
    await Brand1.setDefault_sale(relatedDefault_sale1);
  }

  const relatedDefault_sale2 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const Brand2 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Brand2?.setDefault_sale) {
    await Brand2.setDefault_sale(relatedDefault_sale2);
  }

  const relatedDefault_sale3 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const Brand3 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 3,
  });
  if (Brand3?.setDefault_sale) {
    await Brand3.setDefault_sale(relatedDefault_sale3);
  }

  const relatedDefault_sale4 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const Brand4 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 4,
  });
  if (Brand4?.setDefault_sale) {
    await Brand4.setDefault_sale(relatedDefault_sale4);
  }
}

async function associateBrandWithLink_rate_card() {
  const relatedLink_rate_card0 = await BrandRateCards.findOne({
    offset: Math.floor(Math.random() * (await BrandRateCards.count())),
  });
  const Brand0 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Brand0?.setLink_rate_card) {
    await Brand0.setLink_rate_card(relatedLink_rate_card0);
  }

  const relatedLink_rate_card1 = await BrandRateCards.findOne({
    offset: Math.floor(Math.random() * (await BrandRateCards.count())),
  });
  const Brand1 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Brand1?.setLink_rate_card) {
    await Brand1.setLink_rate_card(relatedLink_rate_card1);
  }

  const relatedLink_rate_card2 = await BrandRateCards.findOne({
    offset: Math.floor(Math.random() * (await BrandRateCards.count())),
  });
  const Brand2 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Brand2?.setLink_rate_card) {
    await Brand2.setLink_rate_card(relatedLink_rate_card2);
  }

  const relatedLink_rate_card3 = await BrandRateCards.findOne({
    offset: Math.floor(Math.random() * (await BrandRateCards.count())),
  });
  const Brand3 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 3,
  });
  if (Brand3?.setLink_rate_card) {
    await Brand3.setLink_rate_card(relatedLink_rate_card3);
  }

  const relatedLink_rate_card4 = await BrandRateCards.findOne({
    offset: Math.floor(Math.random() * (await BrandRateCards.count())),
  });
  const Brand4 = await Brands.findOne({
    order: [['id', 'ASC']],
    offset: 4,
  });
  if (Brand4?.setLink_rate_card) {
    await Brand4.setLink_rate_card(relatedLink_rate_card4);
  }
}

async function associateBrandRateCardWithClient() {
  const relatedClient0 = await Clients.findOne({
    offset: Math.floor(Math.random() * (await Clients.count())),
  });
  const BrandRateCard0 = await BrandRateCards.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (BrandRateCard0?.setClient) {
    await BrandRateCard0.setClient(relatedClient0);
  }

  const relatedClient1 = await Clients.findOne({
    offset: Math.floor(Math.random() * (await Clients.count())),
  });
  const BrandRateCard1 = await BrandRateCards.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (BrandRateCard1?.setClient) {
    await BrandRateCard1.setClient(relatedClient1);
  }

  const relatedClient2 = await Clients.findOne({
    offset: Math.floor(Math.random() * (await Clients.count())),
  });
  const BrandRateCard2 = await BrandRateCards.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (BrandRateCard2?.setClient) {
    await BrandRateCard2.setClient(relatedClient2);
  }

  const relatedClient3 = await Clients.findOne({
    offset: Math.floor(Math.random() * (await Clients.count())),
  });
  const BrandRateCard3 = await BrandRateCards.findOne({
    order: [['id', 'ASC']],
    offset: 3,
  });
  if (BrandRateCard3?.setClient) {
    await BrandRateCard3.setClient(relatedClient3);
  }

  const relatedClient4 = await Clients.findOne({
    offset: Math.floor(Math.random() * (await Clients.count())),
  });
  const BrandRateCard4 = await BrandRateCards.findOne({
    order: [['id', 'ASC']],
    offset: 4,
  });
  if (BrandRateCard4?.setClient) {
    await BrandRateCard4.setClient(relatedClient4);
  }
}

async function associateBrandRateCardWithBrand() {
  const relatedBrand0 = await Brands.findOne({
    offset: Math.floor(Math.random() * (await Brands.count())),
  });
  const BrandRateCard0 = await BrandRateCards.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (BrandRateCard0?.setBrand) {
    await BrandRateCard0.setBrand(relatedBrand0);
  }

  const relatedBrand1 = await Brands.findOne({
    offset: Math.floor(Math.random() * (await Brands.count())),
  });
  const BrandRateCard1 = await BrandRateCards.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (BrandRateCard1?.setBrand) {
    await BrandRateCard1.setBrand(relatedBrand1);
  }

  const relatedBrand2 = await Brands.findOne({
    offset: Math.floor(Math.random() * (await Brands.count())),
  });
  const BrandRateCard2 = await BrandRateCards.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (BrandRateCard2?.setBrand) {
    await BrandRateCard2.setBrand(relatedBrand2);
  }

  const relatedBrand3 = await Brands.findOne({
    offset: Math.floor(Math.random() * (await Brands.count())),
  });
  const BrandRateCard3 = await BrandRateCards.findOne({
    order: [['id', 'ASC']],
    offset: 3,
  });
  if (BrandRateCard3?.setBrand) {
    await BrandRateCard3.setBrand(relatedBrand3);
  }

  const relatedBrand4 = await Brands.findOne({
    offset: Math.floor(Math.random() * (await Brands.count())),
  });
  const BrandRateCard4 = await BrandRateCards.findOne({
    order: [['id', 'ASC']],
    offset: 4,
  });
  if (BrandRateCard4?.setBrand) {
    await BrandRateCard4.setBrand(relatedBrand4);
  }
}

// Similar logic for "relation_many"

async function associateClientWithCountry() {
  const relatedCountry0 = await Countries.findOne({
    offset: Math.floor(Math.random() * (await Countries.count())),
  });
  const Client0 = await Clients.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Client0?.setCountry) {
    await Client0.setCountry(relatedCountry0);
  }

  const relatedCountry1 = await Countries.findOne({
    offset: Math.floor(Math.random() * (await Countries.count())),
  });
  const Client1 = await Clients.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Client1?.setCountry) {
    await Client1.setCountry(relatedCountry1);
  }

  const relatedCountry2 = await Countries.findOne({
    offset: Math.floor(Math.random() * (await Countries.count())),
  });
  const Client2 = await Clients.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Client2?.setCountry) {
    await Client2.setCountry(relatedCountry2);
  }

  const relatedCountry3 = await Countries.findOne({
    offset: Math.floor(Math.random() * (await Countries.count())),
  });
  const Client3 = await Clients.findOne({
    order: [['id', 'ASC']],
    offset: 3,
  });
  if (Client3?.setCountry) {
    await Client3.setCountry(relatedCountry3);
  }

  const relatedCountry4 = await Countries.findOne({
    offset: Math.floor(Math.random() * (await Countries.count())),
  });
  const Client4 = await Clients.findOne({
    order: [['id', 'ASC']],
    offset: 4,
  });
  if (Client4?.setCountry) {
    await Client4.setCountry(relatedCountry4);
  }
}

async function associateClientWithAccount_manager() {
  const relatedAccount_manager0 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const Client0 = await Clients.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Client0?.setAccount_manager) {
    await Client0.setAccount_manager(relatedAccount_manager0);
  }

  const relatedAccount_manager1 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const Client1 = await Clients.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Client1?.setAccount_manager) {
    await Client1.setAccount_manager(relatedAccount_manager1);
  }

  const relatedAccount_manager2 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const Client2 = await Clients.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Client2?.setAccount_manager) {
    await Client2.setAccount_manager(relatedAccount_manager2);
  }

  const relatedAccount_manager3 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const Client3 = await Clients.findOne({
    order: [['id', 'ASC']],
    offset: 3,
  });
  if (Client3?.setAccount_manager) {
    await Client3.setAccount_manager(relatedAccount_manager3);
  }

  const relatedAccount_manager4 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const Client4 = await Clients.findOne({
    order: [['id', 'ASC']],
    offset: 4,
  });
  if (Client4?.setAccount_manager) {
    await Client4.setAccount_manager(relatedAccount_manager4);
  }
}

// Similar logic for "relation_many"

async function associateDefaultActivitiesProductWithDepartment() {
  const relatedDepartment0 = await Departments.findOne({
    offset: Math.floor(Math.random() * (await Departments.count())),
  });
  const DefaultActivitiesProduct0 = await DefaultActivitiesProducts.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (DefaultActivitiesProduct0?.setDepartment) {
    await DefaultActivitiesProduct0.setDepartment(relatedDepartment0);
  }

  const relatedDepartment1 = await Departments.findOne({
    offset: Math.floor(Math.random() * (await Departments.count())),
  });
  const DefaultActivitiesProduct1 = await DefaultActivitiesProducts.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (DefaultActivitiesProduct1?.setDepartment) {
    await DefaultActivitiesProduct1.setDepartment(relatedDepartment1);
  }

  const relatedDepartment2 = await Departments.findOne({
    offset: Math.floor(Math.random() * (await Departments.count())),
  });
  const DefaultActivitiesProduct2 = await DefaultActivitiesProducts.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (DefaultActivitiesProduct2?.setDepartment) {
    await DefaultActivitiesProduct2.setDepartment(relatedDepartment2);
  }

  const relatedDepartment3 = await Departments.findOne({
    offset: Math.floor(Math.random() * (await Departments.count())),
  });
  const DefaultActivitiesProduct3 = await DefaultActivitiesProducts.findOne({
    order: [['id', 'ASC']],
    offset: 3,
  });
  if (DefaultActivitiesProduct3?.setDepartment) {
    await DefaultActivitiesProduct3.setDepartment(relatedDepartment3);
  }

  const relatedDepartment4 = await Departments.findOne({
    offset: Math.floor(Math.random() * (await Departments.count())),
  });
  const DefaultActivitiesProduct4 = await DefaultActivitiesProducts.findOne({
    order: [['id', 'ASC']],
    offset: 4,
  });
  if (DefaultActivitiesProduct4?.setDepartment) {
    await DefaultActivitiesProduct4.setDepartment(relatedDepartment4);
  }
}

async function associateMarketWithCountry() {
  const relatedCountry0 = await Countries.findOne({
    offset: Math.floor(Math.random() * (await Countries.count())),
  });
  const Market0 = await Markets.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Market0?.setCountry) {
    await Market0.setCountry(relatedCountry0);
  }

  const relatedCountry1 = await Countries.findOne({
    offset: Math.floor(Math.random() * (await Countries.count())),
  });
  const Market1 = await Markets.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Market1?.setCountry) {
    await Market1.setCountry(relatedCountry1);
  }

  const relatedCountry2 = await Countries.findOne({
    offset: Math.floor(Math.random() * (await Countries.count())),
  });
  const Market2 = await Markets.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Market2?.setCountry) {
    await Market2.setCountry(relatedCountry2);
  }

  const relatedCountry3 = await Countries.findOne({
    offset: Math.floor(Math.random() * (await Countries.count())),
  });
  const Market3 = await Markets.findOne({
    order: [['id', 'ASC']],
    offset: 3,
  });
  if (Market3?.setCountry) {
    await Market3.setCountry(relatedCountry3);
  }

  const relatedCountry4 = await Countries.findOne({
    offset: Math.floor(Math.random() * (await Countries.count())),
  });
  const Market4 = await Markets.findOne({
    order: [['id', 'ASC']],
    offset: 4,
  });
  if (Market4?.setCountry) {
    await Market4.setCountry(relatedCountry4);
  }
}

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await Organizations.bulkCreate(OrganizationsData);

    await Brands.bulkCreate(BrandsData);

    await BrandRateCards.bulkCreate(BrandRateCardsData);

    await ClientOrders.bulkCreate(ClientOrdersData);

    await Countries.bulkCreate(CountriesData);

    await Clients.bulkCreate(ClientsData);

    await DefaultActivitiesProducts.bulkCreate(DefaultActivitiesProductsData);

    await Departments.bulkCreate(DepartmentsData);

    await Markets.bulkCreate(MarketsData);

    await OutworkLineItems.bulkCreate(OutworkLineItemsData);

    await Reports.bulkCreate(ReportsData);

    await SavedAdaptations.bulkCreate(SavedAdaptationsData);

    await SpecsDatabase.bulkCreate(SpecsDatabaseData);

    await Suppliers.bulkCreate(SuppliersData);

    await SupportingDocuments.bulkCreate(SupportingDocumentsData);

    await TrackingCategories.bulkCreate(TrackingCategoriesData);

    await ZipflowWorkflows.bulkCreate(ZipflowWorkflowsData);

    await Quotes.bulkCreate(QuotesData);

    await AdbuilderAssets.bulkCreate(AdbuilderAssetsData);

    await ClientDam.bulkCreate(ClientDamData);

    await Outwork.bulkCreate(OutworkData);

    await AdbuilderTemplates.bulkCreate(AdbuilderTemplatesData);

    await Campaigns.bulkCreate(CampaignsData);

    await Promise.all([
      // Similar logic for "relation_many"

      await associateBrandWithClient(),

      await associateBrandWithDefault_account_manager(),

      await associateBrandWithDefault_sale(),

      await associateBrandWithLink_rate_card(),

      await associateBrandRateCardWithClient(),

      await associateBrandRateCardWithBrand(),

      // Similar logic for "relation_many"

      await associateClientWithCountry(),

      await associateClientWithAccount_manager(),

      // Similar logic for "relation_many"

      await associateDefaultActivitiesProductWithDepartment(),

      await associateMarketWithCountry(),
    ]);
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.bulkDelete('organizations', null, {});

    await queryInterface.bulkDelete('brands', null, {});

    await queryInterface.bulkDelete('brand_rate_cards', null, {});

    await queryInterface.bulkDelete('client_orders', null, {});

    await queryInterface.bulkDelete('countries', null, {});

    await queryInterface.bulkDelete('clients', null, {});

    await queryInterface.bulkDelete('default_activities_products', null, {});

    await queryInterface.bulkDelete('departments', null, {});

    await queryInterface.bulkDelete('markets', null, {});

    await queryInterface.bulkDelete('outwork_line_items', null, {});

    await queryInterface.bulkDelete('reports', null, {});

    await queryInterface.bulkDelete('saved_adaptations', null, {});

    await queryInterface.bulkDelete('specs_database', null, {});

    await queryInterface.bulkDelete('suppliers', null, {});

    await queryInterface.bulkDelete('supporting_documents', null, {});

    await queryInterface.bulkDelete('tracking_categories', null, {});

    await queryInterface.bulkDelete('zipflow_workflows', null, {});

    await queryInterface.bulkDelete('quotes', null, {});

    await queryInterface.bulkDelete('adbuilder_assets', null, {});

    await queryInterface.bulkDelete('client_dam', null, {});

    await queryInterface.bulkDelete('outwork', null, {});

    await queryInterface.bulkDelete('adbuilder_templates', null, {});

    await queryInterface.bulkDelete('campaigns', null, {});
  },
};
