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

const Claims = db.claims;

const Clients = db.clients;

const CompanyProfiles = db.company_profiles;

const Drivers = db.drivers;

const Expenses = db.expenses;

const Guarantees = db.guarantees;

const Infractions = db.infractions;

const InvoicesSaas = db.invoices_saas;

const Maintenance = db.maintenance;

const Payments = db.payments;

const Plans = db.plans;

const Reservations = db.reservations;

const Subscriptions = db.subscriptions;

const Tenants = db.tenants;

const VehicleConditions = db.vehicle_conditions;

const Vehicles = db.vehicles;

const Agences = db.agences;

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

    // type code here for "relation_one" field

    // type code here for "relation_one" field

    category: 'Paul Ehrlich',

    description: 'Rudolf Virchow',

    status: 'ouverte',

    attachments: 'Charles Sherrington',

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

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    // type code here for "relation_one" field

    category: 'Konrad Lorenz',

    description: 'Erwin Schrodinger',

    status: 'resolue',

    attachments: 'Stephen Hawking',

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

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    // type code here for "relation_one" field

    category: 'Alfred Binet',

    description: 'Max Delbruck',

    status: 'resolue',

    attachments: 'August Kekule',

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

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

    first_name: 'Lucretius',

    last_name: 'Michael Faraday',

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

    phone: 'Dmitri Mendeleev',

    address: 'Ernest Rutherford',

    id_type: 'Passeport',

    id_number: 'B. F. Skinner',

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

    license_number: 'Gregor Mendel',

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

    id_copy_url: 'Galileo Galilei',

    license_copy_url: 'Johannes Kepler',

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

  {
    // type code here for "relation_one" field

    first_name: 'Nicolaus Copernicus',

    last_name: 'Louis Victor de Broglie',

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

    phone: 'Alexander Fleming',

    address: 'William Bayliss',

    id_type: 'Passeport',

    id_number: 'Carl Linnaeus',

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

    license_number: 'John Bardeen',

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

    id_copy_url: 'Jonas Salk',

    license_copy_url: 'Justus Liebig',

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

  {
    // type code here for "relation_one" field

    first_name: 'Murray Gell-Mann',

    last_name: 'Johannes Kepler',

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

    phone: 'William Herschel',

    address: 'John von Neumann',

    id_type: 'Passeport',

    id_number: 'Isaac Newton',

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

    license_number: 'Ernest Rutherford',

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

    id_copy_url: 'Sigmund Freud',

    license_copy_url: 'Trofim Lysenko',

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

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

    legal_name: 'Carline Rabat SARL',

    address: '123 Rue de Rabat, Rabat',

    phone: '+212600000001',

    email: 'contact@carline.ma',

    website: 'www.carline.ma',

    rc: 'RC123456',

    ice: 'ICE123456',

    patente: 'PAT123456',

    logo_url: 'https://example.com/logo_carline.png',

    pdf_footer: 'Carline Rabat SARL, Tous droits réservés.',

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

  {
    // type code here for "relation_one" field

    legal_name: 'AutoMarrakech SARL',

    address: '456 Avenue de Marrakech, Marrakech',

    phone: '+212600000002',

    email: 'contact@automarrakech.ma',

    website: 'www.automarrakech.ma',

    rc: 'RC654321',

    ice: 'ICE654321',

    patente: 'PAT654321',

    logo_url: 'https://example.com/logo_automarrakech.png',

    pdf_footer: 'AutoMarrakech SARL, Tous droits réservés.',

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

  {
    // type code here for "relation_one" field

    legal_name: 'NordRent Tanger SARL',

    address: '789 Boulevard de Tanger, Tanger',

    phone: '+212600000003',

    email: 'contact@nordrent.ma',

    website: 'www.nordrent.ma',

    rc: 'RC789012',

    ice: 'ICE789012',

    patente: 'PAT789012',

    logo_url: 'https://example.com/logo_nordrent.png',

    pdf_footer: 'NordRent Tanger SARL, Tous droits réservés.',

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

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

    // type code here for "relation_one" field

    full_name: 'Emil Fischer',

    license_number: 'Albert Einstein',

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

    id_copy_url: 'Edwin Hubble',

    license_copy_url: 'Emil Fischer',

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

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    full_name: 'Gertrude Belle Elion',

    license_number: 'Louis Pasteur',

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

    id_copy_url: 'Ernest Rutherford',

    license_copy_url: 'Franz Boas',

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

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    full_name: 'Louis Pasteur',

    license_number: 'Ernest Rutherford',

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

    id_copy_url: 'Leonard Euler',

    license_copy_url: 'Albert Einstein',

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

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

    type: 'depense',

    category: 'Hans Bethe',

    amount: 12.95,

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

    // type code here for "relation_one" field

    note: 'Hans Selye',

    attachment_url: 'Ernst Haeckel',

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

  {
    // type code here for "relation_one" field

    type: 'depense',

    category: 'Rudolf Virchow',

    amount: 65.18,

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

    // type code here for "relation_one" field

    note: 'Ernest Rutherford',

    attachment_url: 'Paul Dirac',

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

  {
    // type code here for "relation_one" field

    type: 'revenu',

    category: 'Gregor Mendel',

    amount: 76.77,

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

    // type code here for "relation_one" field

    note: 'William Herschel',

    attachment_url: 'Richard Feynman',

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

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

    // type code here for "relation_one" field

    method: 'especes',

    amount: 95.59,

    status: 'retenue',

    proof_url: 'Louis Pasteur',

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

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    method: 'cheque',

    amount: 22.66,

    status: 'restituee',

    proof_url: 'James Clerk Maxwell',

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

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    method: 'especes',

    amount: 79.03,

    status: 'retenue',

    proof_url: 'Emil Kraepelin',

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

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

    // type code here for "relation_one" field

    // type code here for "relation_one" field

    ref_no: 'Neils Bohr',

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

    location: 'Frederick Sanger',

    amount: 99.05,

    status: 'ouverte',

    proof_url: 'Paul Ehrlich',

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

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    // type code here for "relation_one" field

    ref_no: 'Sigmund Freud',

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

    location: 'Charles Darwin',

    amount: 47.69,

    status: 'payee',

    proof_url: 'Joseph J. Thomson',

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

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    // type code here for "relation_one" field

    ref_no: 'Charles Lyell',

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

    location: 'Charles Lyell',

    amount: 33.18,

    status: 'conteste',

    proof_url: 'Hans Selye',

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

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

    // type code here for "relation_one" field

    invoice_number: 'J. Robert Oppenheimer',

    amount_mad: 72.63,

    status: 'paid',

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

    pdf_url: 'Sheldon Glashow',

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

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    invoice_number: 'Emil Kraepelin',

    amount_mad: 41.61,

    status: 'void',

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

    pdf_url: 'Isaac Newton',

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

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    invoice_number: 'Hans Bethe',

    amount_mad: 98.49,

    status: 'paid',

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

    pdf_url: 'Ernst Mayr',

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

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

    // type code here for "relation_one" field

    type: 'corrective',

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

    odometer: 2,

    cost: 61.92,

    notes: 'Albert Einstein',

    attachments: 'Marie Curie',

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

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    type: 'corrective',

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

    odometer: 2,

    cost: 85.71,

    notes: 'Joseph J. Thomson',

    attachments: 'Trofim Lysenko',

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

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    type: 'preventive',

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

    odometer: 4,

    cost: 32.78,

    notes: 'Charles Darwin',

    attachments: 'Max von Laue',

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

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

    // type code here for "relation_one" field

    method: 'carte',

    amount: 73.38,

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

    receipt_ref: 'Sheldon Glashow',

    attachment_url: 'Michael Faraday',

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

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    method: 'cheque',

    amount: 83.43,

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

    receipt_ref: 'Edward O. Wilson',

    attachment_url: 'Trofim Lysenko',

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

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    method: 'cheque',

    amount: 56.61,

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

    receipt_ref: 'Tycho Brahe',

    attachment_url: 'Galileo Galilei',

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

const PlansData = [
  {
    code: 'BASIC199',

    name: 'Basic',

    period: 'yearly',

    price_mad: 199,

    limits: '{users:1,reservations_per_month:50,storage_mb:500}',

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

  {
    code: 'PRO399',

    name: 'Pro',

    period: 'monthly',

    price_mad: 399,

    limits: '{users:5,reservations_per_month:200,storage_mb:2000}',

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

  {
    code: 'PREMIUM799',

    name: 'Premium',

    period: 'yearly',

    price_mad: 799,

    limits: '{users:-1,reservations_per_month:-1,storage_mb:-1}',

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

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

    ref: 'Jonas Salk',

    status_manual: 'confirmee',

    status_computed: 'terminee',

    // type code here for "relation_one" field

    // type code here for "relation_one" field

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

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

    pickup_address: 'Pierre Simon de Laplace',

    return_address: 'Lucretius',

    options: 'Pierre Simon de Laplace',

    days: 9,

    price_per_day: 86.54,

    total_price: 91.84,

    paid_amount: 77.91,

    balance_amount: 72.26,

    guarantee_method: 'especes',

    guarantee_amount: 79.03,

    guarantee_status: 'retenue',

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

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

    notes: 'Paul Ehrlich',

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

  {
    // type code here for "relation_one" field

    ref: 'Enrico Fermi',

    status_manual: 'en_attente',

    status_computed: 'en_retard',

    // type code here for "relation_one" field

    // type code here for "relation_one" field

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

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

    pickup_address: 'Ernest Rutherford',

    return_address: 'Louis Victor de Broglie',

    options: 'Theodosius Dobzhansky',

    days: 7,

    price_per_day: 80.85,

    total_price: 71.52,

    paid_amount: 17.69,

    balance_amount: 38.02,

    guarantee_method: 'especes',

    guarantee_amount: 15.99,

    guarantee_status: 'restituee',

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

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

    notes: 'Francis Crick',

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

  {
    // type code here for "relation_one" field

    ref: 'Emil Kraepelin',

    status_manual: 'annulee',

    status_computed: 'a_restituer',

    // type code here for "relation_one" field

    // type code here for "relation_one" field

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

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

    pickup_address: 'Sheldon Glashow',

    return_address: 'Gustav Kirchhoff',

    options: 'Claude Bernard',

    days: 6,

    price_per_day: 54.68,

    total_price: 90.09,

    paid_amount: 18.78,

    balance_amount: 98.18,

    guarantee_method: 'especes',

    guarantee_amount: 21.65,

    guarantee_status: 'retenue',

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

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

    notes: 'Ludwig Boltzmann',

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

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

    // type code here for "relation_one" field

    status: 'past_due',

    current_period_start: new Date('2023-01-01T00:00:00Z'),

    current_period_end: new Date('2023-12-31T00:00:00Z'),

    payment_provider: 'none',

    provider_customer_id: 'cust_001',

    provider_subscription_id: 'sub_001',

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

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    status: 'past_due',

    current_period_start: new Date('2023-01-01T00:00:00Z'),

    current_period_end: new Date('2023-12-31T00:00:00Z'),

    payment_provider: 'manual',

    provider_customer_id: 'cust_002',

    provider_subscription_id: 'sub_002',

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

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    status: 'canceled',

    current_period_start: new Date('2023-01-01T00:00:00Z'),

    current_period_end: new Date('2023-12-31T00:00:00Z'),

    payment_provider: 'none',

    provider_customer_id: 'cust_003',

    provider_subscription_id: 'sub_003',

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

const TenantsData = [
  {
    name: 'Carline Rabat',

    subdomain: 'carline',

    status: 'trial',

    // type code here for "relation_one" field

    trial_ends_at: new Date('2023-12-31T00:00:00Z'),

    billing_email: 'billing@carline.ma',

    billing_phone: '+212600000001',

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

  {
    name: 'AutoMarrakech',

    subdomain: 'automarrakech',

    status: 'active',

    // type code here for "relation_one" field

    trial_ends_at: new Date('2023-12-31T00:00:00Z'),

    billing_email: 'billing@automarrakech.ma',

    billing_phone: '+212600000002',

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

  {
    name: 'NordRent Tanger',

    subdomain: 'nordrent',

    status: 'trial',

    // type code here for "relation_one" field

    trial_ends_at: new Date('2023-12-31T00:00:00Z'),

    billing_email: 'billing@nordrent.ma',

    billing_phone: '+212600000003',

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

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

    // type code here for "relation_one" field

    before: 'Alfred Binet',

    after: 'Linus Pauling',

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

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    before: 'Max Planck',

    after: 'Werner Heisenberg',

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

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    before: 'Francis Crick',

    after: 'Richard Feynman',

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

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

    plate_number: 'Galileo Galilei',

    brand: 'William Herschel',

    model: 'Albert Einstein',

    year: 4,

    color: 'Archimedes',

    category: 'berline',

    fuel_type: 'electrique',

    transmission: 'automatique',

    mileage: 9,

    daily_price: 56.66,

    status: 'disponible',

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

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

    photos: 'August Kekule',

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

  {
    // type code here for "relation_one" field

    plate_number: 'Paul Dirac',

    brand: 'Edward Teller',

    model: 'Edward Teller',

    year: 8,

    color: 'George Gaylord Simpson',

    category: 'SUV',

    fuel_type: 'essence',

    transmission: 'manuelle',

    mileage: 9,

    daily_price: 84.24,

    status: 'hors_service',

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

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

    photos: 'Ernest Rutherford',

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

  {
    // type code here for "relation_one" field

    plate_number: 'Erwin Schrodinger',

    brand: 'Ernest Rutherford',

    model: 'Edward Teller',

    year: 7,

    color: 'Anton van Leeuwenhoek',

    category: 'utilitaire',

    fuel_type: 'essence',

    transmission: 'automatique',

    mileage: 2,

    daily_price: 15.16,

    status: 'en_location',

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

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

    photos: 'Trofim Lysenko',

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

const AgencesData = [
  {
    name: 'James Clerk Maxwell',
  },

  {
    name: 'Alfred Binet',
  },

  {
    name: 'Joseph J. Thomson',
  },
];

// Similar logic for "relation_many"

async function associateUserWithAgence() {
  const relatedAgence0 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const User0 = await Users.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (User0?.setAgence) {
    await User0.setAgence(relatedAgence0);
  }

  const relatedAgence1 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const User1 = await Users.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (User1?.setAgence) {
    await User1.setAgence(relatedAgence1);
  }

  const relatedAgence2 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const User2 = await Users.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (User2?.setAgence) {
    await User2.setAgence(relatedAgence2);
  }
}

async function associateClaimWithTenant() {
  const relatedTenant0 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Claim0 = await Claims.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Claim0?.setTenant) {
    await Claim0.setTenant(relatedTenant0);
  }

  const relatedTenant1 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Claim1 = await Claims.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Claim1?.setTenant) {
    await Claim1.setTenant(relatedTenant1);
  }

  const relatedTenant2 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Claim2 = await Claims.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Claim2?.setTenant) {
    await Claim2.setTenant(relatedTenant2);
  }
}

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

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

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

async function associateClaimWithReservation() {
  const relatedReservation0 = await Reservations.findOne({
    offset: Math.floor(Math.random() * (await Reservations.count())),
  });
  const Claim0 = await Claims.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Claim0?.setReservation) {
    await Claim0.setReservation(relatedReservation0);
  }

  const relatedReservation1 = await Reservations.findOne({
    offset: Math.floor(Math.random() * (await Reservations.count())),
  });
  const Claim1 = await Claims.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Claim1?.setReservation) {
    await Claim1.setReservation(relatedReservation1);
  }

  const relatedReservation2 = await Reservations.findOne({
    offset: Math.floor(Math.random() * (await Reservations.count())),
  });
  const Claim2 = await Claims.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Claim2?.setReservation) {
    await Claim2.setReservation(relatedReservation2);
  }
}

async function associateClaimWithAgence() {
  const relatedAgence0 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Claim0 = await Claims.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Claim0?.setAgence) {
    await Claim0.setAgence(relatedAgence0);
  }

  const relatedAgence1 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Claim1 = await Claims.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Claim1?.setAgence) {
    await Claim1.setAgence(relatedAgence1);
  }

  const relatedAgence2 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Claim2 = await Claims.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Claim2?.setAgence) {
    await Claim2.setAgence(relatedAgence2);
  }
}

async function associateClientWithTenant() {
  const relatedTenant0 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Client0 = await Clients.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Client0?.setTenant) {
    await Client0.setTenant(relatedTenant0);
  }

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

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

async function associateClientWithAgence() {
  const relatedAgence0 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Client0 = await Clients.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Client0?.setAgence) {
    await Client0.setAgence(relatedAgence0);
  }

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

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

async function associateCompanyProfileWithTenant() {
  const relatedTenant0 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const CompanyProfile0 = await CompanyProfiles.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (CompanyProfile0?.setTenant) {
    await CompanyProfile0.setTenant(relatedTenant0);
  }

  const relatedTenant1 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const CompanyProfile1 = await CompanyProfiles.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (CompanyProfile1?.setTenant) {
    await CompanyProfile1.setTenant(relatedTenant1);
  }

  const relatedTenant2 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const CompanyProfile2 = await CompanyProfiles.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (CompanyProfile2?.setTenant) {
    await CompanyProfile2.setTenant(relatedTenant2);
  }
}

async function associateCompanyProfileWithAgence() {
  const relatedAgence0 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const CompanyProfile0 = await CompanyProfiles.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (CompanyProfile0?.setAgence) {
    await CompanyProfile0.setAgence(relatedAgence0);
  }

  const relatedAgence1 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const CompanyProfile1 = await CompanyProfiles.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (CompanyProfile1?.setAgence) {
    await CompanyProfile1.setAgence(relatedAgence1);
  }

  const relatedAgence2 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const CompanyProfile2 = await CompanyProfiles.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (CompanyProfile2?.setAgence) {
    await CompanyProfile2.setAgence(relatedAgence2);
  }
}

async function associateDriverWithTenant() {
  const relatedTenant0 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Driver0 = await Drivers.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Driver0?.setTenant) {
    await Driver0.setTenant(relatedTenant0);
  }

  const relatedTenant1 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Driver1 = await Drivers.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Driver1?.setTenant) {
    await Driver1.setTenant(relatedTenant1);
  }

  const relatedTenant2 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Driver2 = await Drivers.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Driver2?.setTenant) {
    await Driver2.setTenant(relatedTenant2);
  }
}

async function associateDriverWithReservation() {
  const relatedReservation0 = await Reservations.findOne({
    offset: Math.floor(Math.random() * (await Reservations.count())),
  });
  const Driver0 = await Drivers.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Driver0?.setReservation) {
    await Driver0.setReservation(relatedReservation0);
  }

  const relatedReservation1 = await Reservations.findOne({
    offset: Math.floor(Math.random() * (await Reservations.count())),
  });
  const Driver1 = await Drivers.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Driver1?.setReservation) {
    await Driver1.setReservation(relatedReservation1);
  }

  const relatedReservation2 = await Reservations.findOne({
    offset: Math.floor(Math.random() * (await Reservations.count())),
  });
  const Driver2 = await Drivers.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Driver2?.setReservation) {
    await Driver2.setReservation(relatedReservation2);
  }
}

async function associateDriverWithAgence() {
  const relatedAgence0 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Driver0 = await Drivers.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Driver0?.setAgence) {
    await Driver0.setAgence(relatedAgence0);
  }

  const relatedAgence1 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Driver1 = await Drivers.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Driver1?.setAgence) {
    await Driver1.setAgence(relatedAgence1);
  }

  const relatedAgence2 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Driver2 = await Drivers.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Driver2?.setAgence) {
    await Driver2.setAgence(relatedAgence2);
  }
}

async function associateExpenseWithTenant() {
  const relatedTenant0 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Expense0 = await Expenses.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Expense0?.setTenant) {
    await Expense0.setTenant(relatedTenant0);
  }

  const relatedTenant1 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Expense1 = await Expenses.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Expense1?.setTenant) {
    await Expense1.setTenant(relatedTenant1);
  }

  const relatedTenant2 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Expense2 = await Expenses.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Expense2?.setTenant) {
    await Expense2.setTenant(relatedTenant2);
  }
}

async function associateExpenseWithVehicle() {
  const relatedVehicle0 = await Vehicles.findOne({
    offset: Math.floor(Math.random() * (await Vehicles.count())),
  });
  const Expense0 = await Expenses.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Expense0?.setVehicle) {
    await Expense0.setVehicle(relatedVehicle0);
  }

  const relatedVehicle1 = await Vehicles.findOne({
    offset: Math.floor(Math.random() * (await Vehicles.count())),
  });
  const Expense1 = await Expenses.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Expense1?.setVehicle) {
    await Expense1.setVehicle(relatedVehicle1);
  }

  const relatedVehicle2 = await Vehicles.findOne({
    offset: Math.floor(Math.random() * (await Vehicles.count())),
  });
  const Expense2 = await Expenses.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Expense2?.setVehicle) {
    await Expense2.setVehicle(relatedVehicle2);
  }
}

async function associateExpenseWithAgence() {
  const relatedAgence0 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Expense0 = await Expenses.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Expense0?.setAgence) {
    await Expense0.setAgence(relatedAgence0);
  }

  const relatedAgence1 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Expense1 = await Expenses.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Expense1?.setAgence) {
    await Expense1.setAgence(relatedAgence1);
  }

  const relatedAgence2 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Expense2 = await Expenses.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Expense2?.setAgence) {
    await Expense2.setAgence(relatedAgence2);
  }
}

async function associateGuaranteeWithTenant() {
  const relatedTenant0 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Guarantee0 = await Guarantees.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Guarantee0?.setTenant) {
    await Guarantee0.setTenant(relatedTenant0);
  }

  const relatedTenant1 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Guarantee1 = await Guarantees.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Guarantee1?.setTenant) {
    await Guarantee1.setTenant(relatedTenant1);
  }

  const relatedTenant2 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Guarantee2 = await Guarantees.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Guarantee2?.setTenant) {
    await Guarantee2.setTenant(relatedTenant2);
  }
}

async function associateGuaranteeWithReservation() {
  const relatedReservation0 = await Reservations.findOne({
    offset: Math.floor(Math.random() * (await Reservations.count())),
  });
  const Guarantee0 = await Guarantees.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Guarantee0?.setReservation) {
    await Guarantee0.setReservation(relatedReservation0);
  }

  const relatedReservation1 = await Reservations.findOne({
    offset: Math.floor(Math.random() * (await Reservations.count())),
  });
  const Guarantee1 = await Guarantees.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Guarantee1?.setReservation) {
    await Guarantee1.setReservation(relatedReservation1);
  }

  const relatedReservation2 = await Reservations.findOne({
    offset: Math.floor(Math.random() * (await Reservations.count())),
  });
  const Guarantee2 = await Guarantees.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Guarantee2?.setReservation) {
    await Guarantee2.setReservation(relatedReservation2);
  }
}

async function associateGuaranteeWithAgence() {
  const relatedAgence0 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Guarantee0 = await Guarantees.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Guarantee0?.setAgence) {
    await Guarantee0.setAgence(relatedAgence0);
  }

  const relatedAgence1 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Guarantee1 = await Guarantees.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Guarantee1?.setAgence) {
    await Guarantee1.setAgence(relatedAgence1);
  }

  const relatedAgence2 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Guarantee2 = await Guarantees.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Guarantee2?.setAgence) {
    await Guarantee2.setAgence(relatedAgence2);
  }
}

async function associateInfractionWithTenant() {
  const relatedTenant0 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Infraction0 = await Infractions.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Infraction0?.setTenant) {
    await Infraction0.setTenant(relatedTenant0);
  }

  const relatedTenant1 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Infraction1 = await Infractions.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Infraction1?.setTenant) {
    await Infraction1.setTenant(relatedTenant1);
  }

  const relatedTenant2 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Infraction2 = await Infractions.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Infraction2?.setTenant) {
    await Infraction2.setTenant(relatedTenant2);
  }
}

async function associateInfractionWithVehicle() {
  const relatedVehicle0 = await Vehicles.findOne({
    offset: Math.floor(Math.random() * (await Vehicles.count())),
  });
  const Infraction0 = await Infractions.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Infraction0?.setVehicle) {
    await Infraction0.setVehicle(relatedVehicle0);
  }

  const relatedVehicle1 = await Vehicles.findOne({
    offset: Math.floor(Math.random() * (await Vehicles.count())),
  });
  const Infraction1 = await Infractions.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Infraction1?.setVehicle) {
    await Infraction1.setVehicle(relatedVehicle1);
  }

  const relatedVehicle2 = await Vehicles.findOne({
    offset: Math.floor(Math.random() * (await Vehicles.count())),
  });
  const Infraction2 = await Infractions.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Infraction2?.setVehicle) {
    await Infraction2.setVehicle(relatedVehicle2);
  }
}

async function associateInfractionWithReservation() {
  const relatedReservation0 = await Reservations.findOne({
    offset: Math.floor(Math.random() * (await Reservations.count())),
  });
  const Infraction0 = await Infractions.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Infraction0?.setReservation) {
    await Infraction0.setReservation(relatedReservation0);
  }

  const relatedReservation1 = await Reservations.findOne({
    offset: Math.floor(Math.random() * (await Reservations.count())),
  });
  const Infraction1 = await Infractions.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Infraction1?.setReservation) {
    await Infraction1.setReservation(relatedReservation1);
  }

  const relatedReservation2 = await Reservations.findOne({
    offset: Math.floor(Math.random() * (await Reservations.count())),
  });
  const Infraction2 = await Infractions.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Infraction2?.setReservation) {
    await Infraction2.setReservation(relatedReservation2);
  }
}

async function associateInfractionWithAgence() {
  const relatedAgence0 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Infraction0 = await Infractions.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Infraction0?.setAgence) {
    await Infraction0.setAgence(relatedAgence0);
  }

  const relatedAgence1 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Infraction1 = await Infractions.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Infraction1?.setAgence) {
    await Infraction1.setAgence(relatedAgence1);
  }

  const relatedAgence2 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Infraction2 = await Infractions.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Infraction2?.setAgence) {
    await Infraction2.setAgence(relatedAgence2);
  }
}

async function associateInvoicesSaaWithTenant() {
  const relatedTenant0 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const InvoicesSaa0 = await InvoicesSaas.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (InvoicesSaa0?.setTenant) {
    await InvoicesSaa0.setTenant(relatedTenant0);
  }

  const relatedTenant1 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const InvoicesSaa1 = await InvoicesSaas.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (InvoicesSaa1?.setTenant) {
    await InvoicesSaa1.setTenant(relatedTenant1);
  }

  const relatedTenant2 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const InvoicesSaa2 = await InvoicesSaas.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (InvoicesSaa2?.setTenant) {
    await InvoicesSaa2.setTenant(relatedTenant2);
  }
}

async function associateInvoicesSaaWithSubscription() {
  const relatedSubscription0 = await Subscriptions.findOne({
    offset: Math.floor(Math.random() * (await Subscriptions.count())),
  });
  const InvoicesSaa0 = await InvoicesSaas.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (InvoicesSaa0?.setSubscription) {
    await InvoicesSaa0.setSubscription(relatedSubscription0);
  }

  const relatedSubscription1 = await Subscriptions.findOne({
    offset: Math.floor(Math.random() * (await Subscriptions.count())),
  });
  const InvoicesSaa1 = await InvoicesSaas.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (InvoicesSaa1?.setSubscription) {
    await InvoicesSaa1.setSubscription(relatedSubscription1);
  }

  const relatedSubscription2 = await Subscriptions.findOne({
    offset: Math.floor(Math.random() * (await Subscriptions.count())),
  });
  const InvoicesSaa2 = await InvoicesSaas.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (InvoicesSaa2?.setSubscription) {
    await InvoicesSaa2.setSubscription(relatedSubscription2);
  }
}

async function associateInvoicesSaaWithAgence() {
  const relatedAgence0 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const InvoicesSaa0 = await InvoicesSaas.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (InvoicesSaa0?.setAgence) {
    await InvoicesSaa0.setAgence(relatedAgence0);
  }

  const relatedAgence1 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const InvoicesSaa1 = await InvoicesSaas.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (InvoicesSaa1?.setAgence) {
    await InvoicesSaa1.setAgence(relatedAgence1);
  }

  const relatedAgence2 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const InvoicesSaa2 = await InvoicesSaas.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (InvoicesSaa2?.setAgence) {
    await InvoicesSaa2.setAgence(relatedAgence2);
  }
}

async function associateMaintenanceWithTenant() {
  const relatedTenant0 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Maintenance0 = await Maintenance.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Maintenance0?.setTenant) {
    await Maintenance0.setTenant(relatedTenant0);
  }

  const relatedTenant1 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Maintenance1 = await Maintenance.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Maintenance1?.setTenant) {
    await Maintenance1.setTenant(relatedTenant1);
  }

  const relatedTenant2 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Maintenance2 = await Maintenance.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Maintenance2?.setTenant) {
    await Maintenance2.setTenant(relatedTenant2);
  }
}

async function associateMaintenanceWithVehicle() {
  const relatedVehicle0 = await Vehicles.findOne({
    offset: Math.floor(Math.random() * (await Vehicles.count())),
  });
  const Maintenance0 = await Maintenance.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Maintenance0?.setVehicle) {
    await Maintenance0.setVehicle(relatedVehicle0);
  }

  const relatedVehicle1 = await Vehicles.findOne({
    offset: Math.floor(Math.random() * (await Vehicles.count())),
  });
  const Maintenance1 = await Maintenance.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Maintenance1?.setVehicle) {
    await Maintenance1.setVehicle(relatedVehicle1);
  }

  const relatedVehicle2 = await Vehicles.findOne({
    offset: Math.floor(Math.random() * (await Vehicles.count())),
  });
  const Maintenance2 = await Maintenance.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Maintenance2?.setVehicle) {
    await Maintenance2.setVehicle(relatedVehicle2);
  }
}

async function associateMaintenanceWithAgence() {
  const relatedAgence0 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Maintenance0 = await Maintenance.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Maintenance0?.setAgence) {
    await Maintenance0.setAgence(relatedAgence0);
  }

  const relatedAgence1 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Maintenance1 = await Maintenance.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Maintenance1?.setAgence) {
    await Maintenance1.setAgence(relatedAgence1);
  }

  const relatedAgence2 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Maintenance2 = await Maintenance.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Maintenance2?.setAgence) {
    await Maintenance2.setAgence(relatedAgence2);
  }
}

async function associatePaymentWithTenant() {
  const relatedTenant0 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Payment0 = await Payments.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Payment0?.setTenant) {
    await Payment0.setTenant(relatedTenant0);
  }

  const relatedTenant1 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Payment1 = await Payments.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Payment1?.setTenant) {
    await Payment1.setTenant(relatedTenant1);
  }

  const relatedTenant2 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Payment2 = await Payments.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Payment2?.setTenant) {
    await Payment2.setTenant(relatedTenant2);
  }
}

async function associatePaymentWithReservation() {
  const relatedReservation0 = await Reservations.findOne({
    offset: Math.floor(Math.random() * (await Reservations.count())),
  });
  const Payment0 = await Payments.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Payment0?.setReservation) {
    await Payment0.setReservation(relatedReservation0);
  }

  const relatedReservation1 = await Reservations.findOne({
    offset: Math.floor(Math.random() * (await Reservations.count())),
  });
  const Payment1 = await Payments.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Payment1?.setReservation) {
    await Payment1.setReservation(relatedReservation1);
  }

  const relatedReservation2 = await Reservations.findOne({
    offset: Math.floor(Math.random() * (await Reservations.count())),
  });
  const Payment2 = await Payments.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Payment2?.setReservation) {
    await Payment2.setReservation(relatedReservation2);
  }
}

async function associatePaymentWithAgence() {
  const relatedAgence0 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Payment0 = await Payments.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Payment0?.setAgence) {
    await Payment0.setAgence(relatedAgence0);
  }

  const relatedAgence1 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Payment1 = await Payments.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Payment1?.setAgence) {
    await Payment1.setAgence(relatedAgence1);
  }

  const relatedAgence2 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Payment2 = await Payments.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Payment2?.setAgence) {
    await Payment2.setAgence(relatedAgence2);
  }
}

async function associatePlanWithAgence() {
  const relatedAgence0 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Plan0 = await Plans.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Plan0?.setAgence) {
    await Plan0.setAgence(relatedAgence0);
  }

  const relatedAgence1 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Plan1 = await Plans.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Plan1?.setAgence) {
    await Plan1.setAgence(relatedAgence1);
  }

  const relatedAgence2 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Plan2 = await Plans.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Plan2?.setAgence) {
    await Plan2.setAgence(relatedAgence2);
  }
}

async function associateReservationWithTenant() {
  const relatedTenant0 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Reservation0 = await Reservations.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Reservation0?.setTenant) {
    await Reservation0.setTenant(relatedTenant0);
  }

  const relatedTenant1 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Reservation1 = await Reservations.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Reservation1?.setTenant) {
    await Reservation1.setTenant(relatedTenant1);
  }

  const relatedTenant2 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Reservation2 = await Reservations.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Reservation2?.setTenant) {
    await Reservation2.setTenant(relatedTenant2);
  }
}

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

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

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

async function associateReservationWithVehicle() {
  const relatedVehicle0 = await Vehicles.findOne({
    offset: Math.floor(Math.random() * (await Vehicles.count())),
  });
  const Reservation0 = await Reservations.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Reservation0?.setVehicle) {
    await Reservation0.setVehicle(relatedVehicle0);
  }

  const relatedVehicle1 = await Vehicles.findOne({
    offset: Math.floor(Math.random() * (await Vehicles.count())),
  });
  const Reservation1 = await Reservations.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Reservation1?.setVehicle) {
    await Reservation1.setVehicle(relatedVehicle1);
  }

  const relatedVehicle2 = await Vehicles.findOne({
    offset: Math.floor(Math.random() * (await Vehicles.count())),
  });
  const Reservation2 = await Reservations.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Reservation2?.setVehicle) {
    await Reservation2.setVehicle(relatedVehicle2);
  }
}

async function associateReservationWithAgence() {
  const relatedAgence0 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Reservation0 = await Reservations.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Reservation0?.setAgence) {
    await Reservation0.setAgence(relatedAgence0);
  }

  const relatedAgence1 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Reservation1 = await Reservations.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Reservation1?.setAgence) {
    await Reservation1.setAgence(relatedAgence1);
  }

  const relatedAgence2 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Reservation2 = await Reservations.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Reservation2?.setAgence) {
    await Reservation2.setAgence(relatedAgence2);
  }
}

async function associateSubscriptionWithTenant() {
  const relatedTenant0 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Subscription0 = await Subscriptions.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Subscription0?.setTenant) {
    await Subscription0.setTenant(relatedTenant0);
  }

  const relatedTenant1 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Subscription1 = await Subscriptions.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Subscription1?.setTenant) {
    await Subscription1.setTenant(relatedTenant1);
  }

  const relatedTenant2 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Subscription2 = await Subscriptions.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Subscription2?.setTenant) {
    await Subscription2.setTenant(relatedTenant2);
  }
}

async function associateSubscriptionWithPlan() {
  const relatedPlan0 = await Plans.findOne({
    offset: Math.floor(Math.random() * (await Plans.count())),
  });
  const Subscription0 = await Subscriptions.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Subscription0?.setPlan) {
    await Subscription0.setPlan(relatedPlan0);
  }

  const relatedPlan1 = await Plans.findOne({
    offset: Math.floor(Math.random() * (await Plans.count())),
  });
  const Subscription1 = await Subscriptions.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Subscription1?.setPlan) {
    await Subscription1.setPlan(relatedPlan1);
  }

  const relatedPlan2 = await Plans.findOne({
    offset: Math.floor(Math.random() * (await Plans.count())),
  });
  const Subscription2 = await Subscriptions.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Subscription2?.setPlan) {
    await Subscription2.setPlan(relatedPlan2);
  }
}

async function associateSubscriptionWithAgence() {
  const relatedAgence0 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Subscription0 = await Subscriptions.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Subscription0?.setAgence) {
    await Subscription0.setAgence(relatedAgence0);
  }

  const relatedAgence1 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Subscription1 = await Subscriptions.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Subscription1?.setAgence) {
    await Subscription1.setAgence(relatedAgence1);
  }

  const relatedAgence2 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Subscription2 = await Subscriptions.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Subscription2?.setAgence) {
    await Subscription2.setAgence(relatedAgence2);
  }
}

async function associateTenantWithPlan() {
  const relatedPlan0 = await Plans.findOne({
    offset: Math.floor(Math.random() * (await Plans.count())),
  });
  const Tenant0 = await Tenants.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Tenant0?.setPlan) {
    await Tenant0.setPlan(relatedPlan0);
  }

  const relatedPlan1 = await Plans.findOne({
    offset: Math.floor(Math.random() * (await Plans.count())),
  });
  const Tenant1 = await Tenants.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Tenant1?.setPlan) {
    await Tenant1.setPlan(relatedPlan1);
  }

  const relatedPlan2 = await Plans.findOne({
    offset: Math.floor(Math.random() * (await Plans.count())),
  });
  const Tenant2 = await Tenants.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Tenant2?.setPlan) {
    await Tenant2.setPlan(relatedPlan2);
  }
}

async function associateTenantWithAgence() {
  const relatedAgence0 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Tenant0 = await Tenants.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Tenant0?.setAgence) {
    await Tenant0.setAgence(relatedAgence0);
  }

  const relatedAgence1 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Tenant1 = await Tenants.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Tenant1?.setAgence) {
    await Tenant1.setAgence(relatedAgence1);
  }

  const relatedAgence2 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Tenant2 = await Tenants.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Tenant2?.setAgence) {
    await Tenant2.setAgence(relatedAgence2);
  }
}

async function associateVehicleConditionWithTenant() {
  const relatedTenant0 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const VehicleCondition0 = await VehicleConditions.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (VehicleCondition0?.setTenant) {
    await VehicleCondition0.setTenant(relatedTenant0);
  }

  const relatedTenant1 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const VehicleCondition1 = await VehicleConditions.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (VehicleCondition1?.setTenant) {
    await VehicleCondition1.setTenant(relatedTenant1);
  }

  const relatedTenant2 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const VehicleCondition2 = await VehicleConditions.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (VehicleCondition2?.setTenant) {
    await VehicleCondition2.setTenant(relatedTenant2);
  }
}

async function associateVehicleConditionWithReservation() {
  const relatedReservation0 = await Reservations.findOne({
    offset: Math.floor(Math.random() * (await Reservations.count())),
  });
  const VehicleCondition0 = await VehicleConditions.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (VehicleCondition0?.setReservation) {
    await VehicleCondition0.setReservation(relatedReservation0);
  }

  const relatedReservation1 = await Reservations.findOne({
    offset: Math.floor(Math.random() * (await Reservations.count())),
  });
  const VehicleCondition1 = await VehicleConditions.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (VehicleCondition1?.setReservation) {
    await VehicleCondition1.setReservation(relatedReservation1);
  }

  const relatedReservation2 = await Reservations.findOne({
    offset: Math.floor(Math.random() * (await Reservations.count())),
  });
  const VehicleCondition2 = await VehicleConditions.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (VehicleCondition2?.setReservation) {
    await VehicleCondition2.setReservation(relatedReservation2);
  }
}

async function associateVehicleConditionWithAgence() {
  const relatedAgence0 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const VehicleCondition0 = await VehicleConditions.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (VehicleCondition0?.setAgence) {
    await VehicleCondition0.setAgence(relatedAgence0);
  }

  const relatedAgence1 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const VehicleCondition1 = await VehicleConditions.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (VehicleCondition1?.setAgence) {
    await VehicleCondition1.setAgence(relatedAgence1);
  }

  const relatedAgence2 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const VehicleCondition2 = await VehicleConditions.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (VehicleCondition2?.setAgence) {
    await VehicleCondition2.setAgence(relatedAgence2);
  }
}

async function associateVehicleWithTenant() {
  const relatedTenant0 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Vehicle0 = await Vehicles.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Vehicle0?.setTenant) {
    await Vehicle0.setTenant(relatedTenant0);
  }

  const relatedTenant1 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Vehicle1 = await Vehicles.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Vehicle1?.setTenant) {
    await Vehicle1.setTenant(relatedTenant1);
  }

  const relatedTenant2 = await Tenants.findOne({
    offset: Math.floor(Math.random() * (await Tenants.count())),
  });
  const Vehicle2 = await Vehicles.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Vehicle2?.setTenant) {
    await Vehicle2.setTenant(relatedTenant2);
  }
}

async function associateVehicleWithAgence() {
  const relatedAgence0 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Vehicle0 = await Vehicles.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Vehicle0?.setAgence) {
    await Vehicle0.setAgence(relatedAgence0);
  }

  const relatedAgence1 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Vehicle1 = await Vehicles.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Vehicle1?.setAgence) {
    await Vehicle1.setAgence(relatedAgence1);
  }

  const relatedAgence2 = await Agences.findOne({
    offset: Math.floor(Math.random() * (await Agences.count())),
  });
  const Vehicle2 = await Vehicles.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Vehicle2?.setAgence) {
    await Vehicle2.setAgence(relatedAgence2);
  }
}

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

    await Clients.bulkCreate(ClientsData);

    await CompanyProfiles.bulkCreate(CompanyProfilesData);

    await Drivers.bulkCreate(DriversData);

    await Expenses.bulkCreate(ExpensesData);

    await Guarantees.bulkCreate(GuaranteesData);

    await Infractions.bulkCreate(InfractionsData);

    await InvoicesSaas.bulkCreate(InvoicesSaasData);

    await Maintenance.bulkCreate(MaintenanceData);

    await Payments.bulkCreate(PaymentsData);

    await Plans.bulkCreate(PlansData);

    await Reservations.bulkCreate(ReservationsData);

    await Subscriptions.bulkCreate(SubscriptionsData);

    await Tenants.bulkCreate(TenantsData);

    await VehicleConditions.bulkCreate(VehicleConditionsData);

    await Vehicles.bulkCreate(VehiclesData);

    await Agences.bulkCreate(AgencesData);

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

      await associateUserWithAgence(),

      await associateClaimWithTenant(),

      await associateClaimWithClient(),

      await associateClaimWithReservation(),

      await associateClaimWithAgence(),

      await associateClientWithTenant(),

      await associateClientWithAgence(),

      await associateCompanyProfileWithTenant(),

      await associateCompanyProfileWithAgence(),

      await associateDriverWithTenant(),

      await associateDriverWithReservation(),

      await associateDriverWithAgence(),

      await associateExpenseWithTenant(),

      await associateExpenseWithVehicle(),

      await associateExpenseWithAgence(),

      await associateGuaranteeWithTenant(),

      await associateGuaranteeWithReservation(),

      await associateGuaranteeWithAgence(),

      await associateInfractionWithTenant(),

      await associateInfractionWithVehicle(),

      await associateInfractionWithReservation(),

      await associateInfractionWithAgence(),

      await associateInvoicesSaaWithTenant(),

      await associateInvoicesSaaWithSubscription(),

      await associateInvoicesSaaWithAgence(),

      await associateMaintenanceWithTenant(),

      await associateMaintenanceWithVehicle(),

      await associateMaintenanceWithAgence(),

      await associatePaymentWithTenant(),

      await associatePaymentWithReservation(),

      await associatePaymentWithAgence(),

      await associatePlanWithAgence(),

      await associateReservationWithTenant(),

      await associateReservationWithClient(),

      await associateReservationWithVehicle(),

      await associateReservationWithAgence(),

      await associateSubscriptionWithTenant(),

      await associateSubscriptionWithPlan(),

      await associateSubscriptionWithAgence(),

      await associateTenantWithPlan(),

      await associateTenantWithAgence(),

      await associateVehicleConditionWithTenant(),

      await associateVehicleConditionWithReservation(),

      await associateVehicleConditionWithAgence(),

      await associateVehicleWithTenant(),

      await associateVehicleWithAgence(),
    ]);
  },

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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