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

const Projects = db.projects;

const ProjectMemberships = db.project_memberships;

const Assets = db.assets;

const AssetVariants = db.asset_variants;

const PresignedUrlRequests = db.presigned_url_requests;

const TourPages = db.tour_pages;

const ProjectAudioTracks = db.project_audio_tracks;

const PublishEvents = db.publish_events;

const PwaCaches = db.pwa_caches;

const AccessLogs = db.access_logs;

const ProjectsData = [
  {
    name: 'Cardiff Arena Tour',
    slug: 'cardiff-arena',
    description: 'Interactive arena tour for visitors and event planners.',
    logo_url: 'https://cdn.platform.com/cardiff/logo.png',
    favicon_url: 'https://cdn.platform.com/cardiff/favicon.ico',
    og_image_url: 'https://cdn.platform.com/cardiff/og.jpg',
    is_deleted: true,
    deleted_at_time: new Date('2026-01-01T00:00:00Z'),
  },
  {
    name: 'Riverside Park Walkthrough',
    slug: 'riverside-park',
    description: 'Offline-ready guided walkthrough for the city park.',
    logo_url: 'https://cdn.platform.com/riverside/logo.png',
    favicon_url: 'https://cdn.platform.com/riverside/favicon.ico',
    og_image_url: 'https://cdn.platform.com/riverside/og.jpg',
    is_deleted: false,
    deleted_at_time: new Date('2026-01-01T00:00:00Z'),
  },
  {
    name: 'Mall Central Experience',
    slug: 'mall-central',
    description: 'Retail complex presentation with navigation and galleries.',
    logo_url: 'https://cdn.platform.com/mall/logo.png',
    favicon_url: 'https://cdn.platform.com/mall/favicon.ico',
    og_image_url: 'https://cdn.platform.com/mall/og.jpg',
    is_deleted: false,
    deleted_at_time: new Date('2026-01-01T00:00:00Z'),
  },
];

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

    // type code here for "relation_one" field

    access_level: 'editor',

    is_active: true,

    invited_at: new Date('2026-02-01T10:00:00Z'),

    accepted_at: new Date('2026-02-01T10:05:00Z'),
  },

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    access_level: 'reviewer',

    is_active: true,

    invited_at: new Date('2026-02-02T09:00:00Z'),

    accepted_at: new Date('2026-02-02T09:10:00Z'),
  },

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    access_level: 'viewer',

    is_active: true,

    invited_at: new Date('2026-02-10T14:00:00Z'),

    accepted_at: new Date('2026-02-10T14:30:00Z'),
  },
];

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

    name: 'Arena Lobby Background',

    asset_type: 'image',

    cdn_url: 'https://cdn.platform.com/cardiff/images/lobby.webp',

    storage_key: 'cardiff/images/lobby.webp',

    mime_type: 'image/webp',

    size_mb: 2.45,

    width_px: 3840,

    height_px: 2160,

    duration_sec: 0.0,

    checksum: 'sha256_lobby_001',

    is_public: true,

    is_deleted: false,

    deleted_at_time: new Date('2026-01-01T00:00:00Z'),
  },

  {
    // type code here for "relation_one" field

    name: 'Hallway Transition Video',

    asset_type: 'video',

    cdn_url: 'https://cdn.platform.com/cardiff/video/transition-hallway.mp4',

    storage_key: 'cardiff/video/transition-hallway.mp4',

    mime_type: 'video/mp4',

    size_mb: 18.2,

    width_px: 1920,

    height_px: 1080,

    duration_sec: 4.8,

    checksum: 'sha256_vid_002',

    is_public: true,

    is_deleted: true,

    deleted_at_time: new Date('2026-01-01T00:00:00Z'),
  },

  {
    // type code here for "relation_one" field

    name: 'Park Ambience Loop',

    asset_type: 'video',

    cdn_url: 'https://cdn.platform.com/riverside/audio/ambience.mp3',

    storage_key: 'riverside/audio/ambience.mp3',

    mime_type: 'audio/mpeg',

    size_mb: 5.6,

    width_px: 0,

    height_px: 0,

    duration_sec: 120.0,

    checksum: 'sha256_aud_003',

    is_public: true,

    is_deleted: true,

    deleted_at_time: new Date('2026-01-01T00:00:00Z'),
  },
];

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

    variant_type: 'mp4_low',

    cdn_url: 'https://cdn.platform.com/cardiff/images/lobby_thumb.webp',

    width_px: 640,

    height_px: 360,

    size_mb: 0.12,
  },

  {
    // type code here for "relation_one" field

    variant_type: 'preview',

    cdn_url: 'https://cdn.platform.com/cardiff/images/lobby_preview.webp',

    width_px: 1280,

    height_px: 720,

    size_mb: 0.35,
  },

  {
    // type code here for "relation_one" field

    variant_type: 'preview',

    cdn_url:
      'https://cdn.platform.com/cardiff/video/transition-hallway_720p.mp4',

    width_px: 1280,

    height_px: 720,

    size_mb: 9.4,
  },
];

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

    // type code here for "relation_one" field

    purpose: 'download',

    asset_type: 'video',

    requested_key: 'cardiff/images/section-a.webp',

    mime_type: 'image/webp',

    requested_size_mb: 3.2,

    expires_at: new Date('2026-03-16T12:30:00Z'),

    status: 'issued',
  },

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    purpose: 'upload',

    asset_type: 'video',

    requested_key: 'cardiff/video/transition-gate.mp4',

    mime_type: 'video/mp4',

    requested_size_mb: 28.0,

    expires_at: new Date('2026-03-16T12:35:00Z'),

    status: 'issued',
  },

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    purpose: 'upload',

    asset_type: 'audio',

    requested_key: 'riverside/audio/ambience.mp3',

    mime_type: 'audio/mpeg',

    requested_size_mb: 6.0,

    expires_at: new Date('2026-03-16T09:10:00Z'),

    status: 'issued',
  },
];

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

    environment: 'stage',

    source_key: 'cardiff_welcome_stage',

    name: 'Welcome',

    slug: 'welcome',

    sort_order: 1,

    background_image_url: 'https://cdn.platform.com/cardiff/images/lobby.webp',

    background_video_url: '',

    background_audio_url: '',

    background_loop: true,

    requires_auth: true,

    ui_schema_json: '{menu:{enabled:false},grid:responsive}',
  },

  {
    // type code here for "relation_one" field

    environment: 'stage',

    source_key: 'cardiff_arena_floor_stage',

    name: 'Arena Floor',

    slug: 'arena-floor',

    sort_order: 2,

    background_image_url: '',

    background_video_url:
      'https://cdn.platform.com/cardiff/video/floor-loop.mp4',

    background_audio_url: '',

    background_loop: true,

    requires_auth: true,

    ui_schema_json: '{safeArea:true}',
  },

  {
    // type code here for "relation_one" field

    environment: 'stage',

    source_key: 'riverside_start_prod',

    name: 'Start',

    slug: 'start',

    sort_order: 1,

    background_image_url:
      'https://cdn.platform.com/riverside/images/start.webp',

    background_video_url: '',

    background_audio_url: '',

    background_loop: true,

    requires_auth: true,

    ui_schema_json: '{hint:Swipe to explore}',
  },
];

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

    environment: 'dev',

    source_key: 'cardiff_global_stage',

    name: 'Arena Ambient',

    slug: 'arena-ambient',

    url: 'https://cdn.platform.com/cardiff/audio/ambient.mp3',

    loop: true,

    volume: 0.6,

    sort_order: 1,

    is_enabled: true,
  },

  {
    // type code here for "relation_one" field

    environment: 'production',

    source_key: 'riverside_global_prod',

    name: 'Park Ambience',

    slug: 'park-ambience',

    url: 'https://cdn.platform.com/riverside/audio/ambience.mp3',

    loop: true,

    volume: 0.7,

    sort_order: 1,

    is_enabled: true,
  },

  {
    // type code here for "relation_one" field

    environment: 'dev',

    source_key: 'mall_global_dev',

    name: 'Soft Lounge',

    slug: 'soft-lounge',

    url: 'https://cdn.platform.com/mall/audio/lounge.mp3',

    loop: true,

    volume: 0.5,

    sort_order: 1,

    is_enabled: false,
  },
];

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

    // type code here for "relation_one" field

    from_environment: 'dev',

    to_environment: 'dev',

    started_at: new Date('2026-03-01T10:00:00Z'),

    finished_at: new Date('2026-03-01T10:01:10Z'),

    status: 'queued',

    error_message: '',

    pages_copied: 6,

    transitions_copied: 2,

    audios_copied: 1,
  },

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    from_environment: 'production',

    to_environment: 'production',

    started_at: new Date('2026-03-15T18:00:00Z'),

    finished_at: new Date('2026-03-15T18:02:40Z'),

    status: 'queued',

    error_message:
      'Asset preload list generation failed due to missing CDN URL.',

    pages_copied: 6,

    transitions_copied: 2,

    audios_copied: 1,
  },

  {
    // type code here for "relation_one" field

    // type code here for "relation_one" field

    from_environment: 'dev',

    to_environment: 'production',

    started_at: new Date('2026-02-05T09:00:00Z'),

    finished_at: new Date('2026-02-05T09:01:05Z'),

    status: 'running',

    error_message: '',

    pages_copied: 4,

    transitions_copied: 1,

    audios_copied: 1,
  },
];

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

    environment: 'production',

    cache_version: 'cardiff-stage-v12',

    manifest_json:
      '{name:Cardiff Arena Tour,start_url:/welcome,display:standalone}',

    asset_list_json:
      '{assets:[/images/lobby.webp,/video/transition-hallway.mp4]}',

    generated_at: new Date('2026-03-15T17:40:00Z'),

    is_active: true,
  },

  {
    // type code here for "relation_one" field

    environment: 'stage',

    cache_version: 'riverside-prod-v7',

    manifest_json:
      '{name:Riverside Park Walkthrough,start_url:/start,display:standalone}',

    asset_list_json: '{assets:[/images/start.webp,/audio/ambience.mp3]}',

    generated_at: new Date('2026-02-05T09:02:00Z'),

    is_active: true,
  },

  {
    // type code here for "relation_one" field

    environment: 'stage',

    cache_version: 'seaside-stage-v3',

    manifest_json:
      '{name:Seaside Concert Hall,start_url:/lobby,display:standalone}',

    asset_list_json:
      '{assets:[/images/lobby.webp,/video/spotlight.mp4,/audio/foyer-loop.mp3]}',

    generated_at: new Date('2026-03-05T12:22:00Z'),

    is_active: true,
  },
];

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

    environment: 'stage',

    // type code here for "relation_one" field

    path: '/welcome',

    ip_address: '203.0.113.10',

    user_agent: 'Mozilla/5.0 Chrome/122.0 Desktop',

    accessed_at: new Date('2026-03-14T08:06:10Z'),
  },

  {
    // type code here for "relation_one" field

    environment: 'production',

    // type code here for "relation_one" field

    path: '/admin/projects/cardiff-arena',

    ip_address: '203.0.113.11',

    user_agent: 'Mozilla/5.0 Chrome/122.0 Desktop',

    accessed_at: new Date('2026-03-15T17:55:44Z'),
  },

  {
    // type code here for "relation_one" field

    environment: 'stage',

    // type code here for "relation_one" field

    path: '/start',

    ip_address: '198.51.100.55',

    user_agent: 'Mozilla/5.0 Safari/17.3 iOS',

    accessed_at: new Date('2026-03-12T10:20:00Z'),
  },
];

// Similar logic for "relation_many"

async function associateProjectMembershipWithProject() {
  const relatedProject0 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const ProjectMembership0 = await ProjectMemberships.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (ProjectMembership0?.setProject) {
    await ProjectMembership0.setProject(relatedProject0);
  }

  const relatedProject1 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const ProjectMembership1 = await ProjectMemberships.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (ProjectMembership1?.setProject) {
    await ProjectMembership1.setProject(relatedProject1);
  }

  const relatedProject2 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const ProjectMembership2 = await ProjectMemberships.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (ProjectMembership2?.setProject) {
    await ProjectMembership2.setProject(relatedProject2);
  }
}

async function associateProjectMembershipWithUser() {
  const relatedUser0 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const ProjectMembership0 = await ProjectMemberships.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (ProjectMembership0?.setUser) {
    await ProjectMembership0.setUser(relatedUser0);
  }

  const relatedUser1 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const ProjectMembership1 = await ProjectMemberships.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (ProjectMembership1?.setUser) {
    await ProjectMembership1.setUser(relatedUser1);
  }

  const relatedUser2 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const ProjectMembership2 = await ProjectMemberships.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (ProjectMembership2?.setUser) {
    await ProjectMembership2.setUser(relatedUser2);
  }
}

async function associateAssetWithProject() {
  const relatedProject0 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const Asset0 = await Assets.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (Asset0?.setProject) {
    await Asset0.setProject(relatedProject0);
  }

  const relatedProject1 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const Asset1 = await Assets.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (Asset1?.setProject) {
    await Asset1.setProject(relatedProject1);
  }

  const relatedProject2 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const Asset2 = await Assets.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (Asset2?.setProject) {
    await Asset2.setProject(relatedProject2);
  }
}

async function associateAssetVariantWithAsset() {
  const relatedAsset0 = await Assets.findOne({
    offset: Math.floor(Math.random() * (await Assets.count())),
  });
  const AssetVariant0 = await AssetVariants.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (AssetVariant0?.setAsset) {
    await AssetVariant0.setAsset(relatedAsset0);
  }

  const relatedAsset1 = await Assets.findOne({
    offset: Math.floor(Math.random() * (await Assets.count())),
  });
  const AssetVariant1 = await AssetVariants.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (AssetVariant1?.setAsset) {
    await AssetVariant1.setAsset(relatedAsset1);
  }

  const relatedAsset2 = await Assets.findOne({
    offset: Math.floor(Math.random() * (await Assets.count())),
  });
  const AssetVariant2 = await AssetVariants.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (AssetVariant2?.setAsset) {
    await AssetVariant2.setAsset(relatedAsset2);
  }
}

async function associatePresignedUrlRequestWithProject() {
  const relatedProject0 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const PresignedUrlRequest0 = await PresignedUrlRequests.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (PresignedUrlRequest0?.setProject) {
    await PresignedUrlRequest0.setProject(relatedProject0);
  }

  const relatedProject1 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const PresignedUrlRequest1 = await PresignedUrlRequests.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (PresignedUrlRequest1?.setProject) {
    await PresignedUrlRequest1.setProject(relatedProject1);
  }

  const relatedProject2 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const PresignedUrlRequest2 = await PresignedUrlRequests.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (PresignedUrlRequest2?.setProject) {
    await PresignedUrlRequest2.setProject(relatedProject2);
  }
}

async function associatePresignedUrlRequestWithUser() {
  const relatedUser0 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const PresignedUrlRequest0 = await PresignedUrlRequests.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (PresignedUrlRequest0?.setUser) {
    await PresignedUrlRequest0.setUser(relatedUser0);
  }

  const relatedUser1 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const PresignedUrlRequest1 = await PresignedUrlRequests.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (PresignedUrlRequest1?.setUser) {
    await PresignedUrlRequest1.setUser(relatedUser1);
  }

  const relatedUser2 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const PresignedUrlRequest2 = await PresignedUrlRequests.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (PresignedUrlRequest2?.setUser) {
    await PresignedUrlRequest2.setUser(relatedUser2);
  }
}

async function associateTourPageWithProject() {
  const relatedProject0 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const TourPage0 = await TourPages.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (TourPage0?.setProject) {
    await TourPage0.setProject(relatedProject0);
  }

  const relatedProject1 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const TourPage1 = await TourPages.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (TourPage1?.setProject) {
    await TourPage1.setProject(relatedProject1);
  }

  const relatedProject2 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const TourPage2 = await TourPages.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (TourPage2?.setProject) {
    await TourPage2.setProject(relatedProject2);
  }
}

async function associateProjectAudioTrackWithProject() {
  const relatedProject0 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const ProjectAudioTrack0 = await ProjectAudioTracks.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (ProjectAudioTrack0?.setProject) {
    await ProjectAudioTrack0.setProject(relatedProject0);
  }

  const relatedProject1 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const ProjectAudioTrack1 = await ProjectAudioTracks.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (ProjectAudioTrack1?.setProject) {
    await ProjectAudioTrack1.setProject(relatedProject1);
  }

  const relatedProject2 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const ProjectAudioTrack2 = await ProjectAudioTracks.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (ProjectAudioTrack2?.setProject) {
    await ProjectAudioTrack2.setProject(relatedProject2);
  }
}

async function associatePublishEventWithProject() {
  const relatedProject0 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const PublishEvent0 = await PublishEvents.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (PublishEvent0?.setProject) {
    await PublishEvent0.setProject(relatedProject0);
  }

  const relatedProject1 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const PublishEvent1 = await PublishEvents.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (PublishEvent1?.setProject) {
    await PublishEvent1.setProject(relatedProject1);
  }

  const relatedProject2 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const PublishEvent2 = await PublishEvents.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (PublishEvent2?.setProject) {
    await PublishEvent2.setProject(relatedProject2);
  }
}

async function associatePublishEventWithUser() {
  const relatedUser0 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const PublishEvent0 = await PublishEvents.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (PublishEvent0?.setUser) {
    await PublishEvent0.setUser(relatedUser0);
  }

  const relatedUser1 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const PublishEvent1 = await PublishEvents.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (PublishEvent1?.setUser) {
    await PublishEvent1.setUser(relatedUser1);
  }

  const relatedUser2 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const PublishEvent2 = await PublishEvents.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (PublishEvent2?.setUser) {
    await PublishEvent2.setUser(relatedUser2);
  }
}

async function associatePwaCacheWithProject() {
  const relatedProject0 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const PwaCache0 = await PwaCaches.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (PwaCache0?.setProject) {
    await PwaCache0.setProject(relatedProject0);
  }

  const relatedProject1 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const PwaCache1 = await PwaCaches.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (PwaCache1?.setProject) {
    await PwaCache1.setProject(relatedProject1);
  }

  const relatedProject2 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const PwaCache2 = await PwaCaches.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (PwaCache2?.setProject) {
    await PwaCache2.setProject(relatedProject2);
  }
}

async function associateAccessLogWithProject() {
  const relatedProject0 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const AccessLog0 = await AccessLogs.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (AccessLog0?.setProject) {
    await AccessLog0.setProject(relatedProject0);
  }

  const relatedProject1 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const AccessLog1 = await AccessLogs.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (AccessLog1?.setProject) {
    await AccessLog1.setProject(relatedProject1);
  }

  const relatedProject2 = await Projects.findOne({
    offset: Math.floor(Math.random() * (await Projects.count())),
  });
  const AccessLog2 = await AccessLogs.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (AccessLog2?.setProject) {
    await AccessLog2.setProject(relatedProject2);
  }
}

async function associateAccessLogWithUser() {
  const relatedUser0 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const AccessLog0 = await AccessLogs.findOne({
    order: [['id', 'ASC']],
    offset: 0,
  });
  if (AccessLog0?.setUser) {
    await AccessLog0.setUser(relatedUser0);
  }

  const relatedUser1 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const AccessLog1 = await AccessLogs.findOne({
    order: [['id', 'ASC']],
    offset: 1,
  });
  if (AccessLog1?.setUser) {
    await AccessLog1.setUser(relatedUser1);
  }

  const relatedUser2 = await Users.findOne({
    offset: Math.floor(Math.random() * (await Users.count())),
  });
  const AccessLog2 = await AccessLogs.findOne({
    order: [['id', 'ASC']],
    offset: 2,
  });
  if (AccessLog2?.setUser) {
    await AccessLog2.setUser(relatedUser2);
  }
}

module.exports = {
  up: async () => {
    // Keep production-like schema strict; skip auto sample payload inserts by default.
    if (process.env.ENABLE_SAMPLE_DATA !== 'true') {
      return;
    }

    await Projects.bulkCreate(ProjectsData);

    await ProjectMemberships.bulkCreate(ProjectMembershipsData);

    await Assets.bulkCreate(AssetsData);

    await AssetVariants.bulkCreate(AssetVariantsData);

    await PresignedUrlRequests.bulkCreate(PresignedUrlRequestsData);

    await TourPages.bulkCreate(TourPagesData);

    await ProjectAudioTracks.bulkCreate(ProjectAudioTracksData);

    await PublishEvents.bulkCreate(PublishEventsData);

    await PwaCaches.bulkCreate(PwaCachesData);

    await AccessLogs.bulkCreate(AccessLogsData);

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

      await associateProjectMembershipWithProject(),

      await associateProjectMembershipWithUser(),

      await associateAssetWithProject(),

      await associateAssetVariantWithAsset(),

      await associatePresignedUrlRequestWithProject(),

      await associatePresignedUrlRequestWithUser(),

      await associateTourPageWithProject(),

      await associateProjectAudioTrackWithProject(),

      await associatePublishEventWithProject(),

      await associatePublishEventWithUser(),

      await associatePwaCacheWithProject(),

      await associateAccessLogWithProject(),

      await associateAccessLogWithUser(),
    ]);
  },

  down: async (queryInterface) => {
    if (process.env.ENABLE_SAMPLE_DATA !== 'true') {
      return;
    }

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

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

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

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

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

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

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

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

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

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