import { v4 as uuid } from 'uuid';
import {
  Op,
  type CreationAttributes,
  type QueryInterface,
} from 'sequelize';
import type { ContentCatalog } from '@/db/models/content_catalog';
import { CONTENT_CATALOG_SEED_PAYLOADS } from './content-catalog-data/content-catalog-seed-payloads';

const CONTENT_CATALOG_SEED_ROWS = Object.freeze([
  { content_type: 'classroom-strategies', payload: CONTENT_CATALOG_SEED_PAYLOADS.classroomStrategies },
  { content_type: 'safety-qbs-quiz', payload: CONTENT_CATALOG_SEED_PAYLOADS.safetyQbsQuiz },
  { content_type: 'sign-language-items', payload: CONTENT_CATALOG_SEED_PAYLOADS.signLanguageItems },
  { content_type: 'sign-language-page-content', payload: CONTENT_CATALOG_SEED_PAYLOADS.signLanguagePageContent },
  { content_type: 'regulation-zones', payload: CONTENT_CATALOG_SEED_PAYLOADS.regulationZones },
  { content_type: 'zones-of-regulation-page-content', payload: CONTENT_CATALOG_SEED_PAYLOADS.zonesOfRegulationPageContent },
  { content_type: 'dashboard-teacher-images', payload: CONTENT_CATALOG_SEED_PAYLOADS.dashboardTeacherImages },
  { content_type: 'dashboard-encouraging-quotes', payload: CONTENT_CATALOG_SEED_PAYLOADS.dashboardEncouragingQuotes },
  { content_type: 'dashboard-compliance-items', payload: CONTENT_CATALOG_SEED_PAYLOADS.dashboardComplianceItems },
  { content_type: 'dashboard-sign-of-week', payload: CONTENT_CATALOG_SEED_PAYLOADS.dashboardSignOfWeek },
  { content_type: 'parent-message-templates', payload: CONTENT_CATALOG_SEED_PAYLOADS.parentMessageTemplates },
  { content_type: 'community-organizations', payload: CONTENT_CATALOG_SEED_PAYLOADS.communityOrganizations },
  { content_type: 'vocational-opportunities', payload: CONTENT_CATALOG_SEED_PAYLOADS.vocationalOpportunities },
  { content_type: 'emotional-intelligence-assessment-questions', payload: CONTENT_CATALOG_SEED_PAYLOADS.emotionalIntelligenceAssessmentQuestions },
  { content_type: 'emotional-intelligence-weekly-topics', payload: CONTENT_CATALOG_SEED_PAYLOADS.emotionalIntelligenceWeeklyTopics },
  { content_type: 'emotional-intelligence-growth-tips', payload: CONTENT_CATALOG_SEED_PAYLOADS.emotionalIntelligenceGrowthTips },
  { content_type: 'emotional-intelligence-team-wellness-metrics', payload: CONTENT_CATALOG_SEED_PAYLOADS.emotionalIntelligenceTeamWellnessMetrics },
  { content_type: 'personality-quiz-questions', payload: CONTENT_CATALOG_SEED_PAYLOADS.personalityQuizQuestions },
  { content_type: 'personality-types', payload: CONTENT_CATALOG_SEED_PAYLOADS.personalityTypes },
  { content_type: 'esa-funding-content', payload: CONTENT_CATALOG_SEED_PAYLOADS.esaFundingContent },
  // `safety-protocols` is no longer served from the content catalog — safety
  // protocols are now seeded into `policy_documents` (see the policy-documents
  // seeder), which the Safety Protocols page reads.
  { content_type: 'classroom-timer-backgrounds', payload: CONTENT_CATALOG_SEED_PAYLOADS.classroomTimerBackgrounds },
  { content_type: 'classroom-timer-sounds', payload: CONTENT_CATALOG_SEED_PAYLOADS.classroomTimerSounds },
  { content_type: 'classroom-timer-presets', payload: CONTENT_CATALOG_SEED_PAYLOADS.classroomTimerPresets },
  { content_type: 'classroom-timer-tips', payload: CONTENT_CATALOG_SEED_PAYLOADS.classroomTimerTips },
  { content_type: 'personality-quiz-features', payload: CONTENT_CATALOG_SEED_PAYLOADS.personalityQuizFeatures },
  { content_type: 'emotional-intelligence-weekly-focus', payload: CONTENT_CATALOG_SEED_PAYLOADS.emotionalIntelligenceWeeklyFocus },
  { content_type: 'personality-workplace-content', payload: CONTENT_CATALOG_SEED_PAYLOADS.personalityWorkplaceContent },
]);

export default {
  up: async (queryInterface: QueryInterface) => {
    const now = new Date();
    const contentTypes = CONTENT_CATALOG_SEED_ROWS.map((row) => row.content_type);

    await queryInterface.bulkDelete('content_catalog', {
      content_type: {
        [Op.in]: contentTypes,
      },
    });

    const rows: CreationAttributes<ContentCatalog>[] =
      CONTENT_CATALOG_SEED_ROWS.map((row) => ({
        id: uuid(),
        content_type: row.content_type,
        // payload is JSONB; bulkInsert needs a serialized value (Postgres casts
        // the JSON text to jsonb on insert).
        payload: JSON.stringify(row.payload),
        active: true,
        importHash: 'content-catalog-' + row.content_type,
        createdAt: now,
        updatedAt: now,
      }));

    await queryInterface.bulkInsert('content_catalog', rows);
  },

  down: async (queryInterface: QueryInterface) => {
    await queryInterface.bulkDelete('content_catalog', {
      content_type: {
        [Op.in]: CONTENT_CATALOG_SEED_ROWS.map((row) => row.content_type),
      },
    });
  },
};
