import { prisma } from '../src/lib/prisma.js';

async function seedAdminFeaturesContentManagement() {
  await prisma.adminFeature.upsert({
    where: { key: 'CONTENT_MANAGEMENT' },
    update: {
      label: 'Content',
      description: 'Manage dropdown content like property types, amenities, regions and cities',
    },
    create: {
      key: 'CONTENT_MANAGEMENT',
      label: 'Content',
      description: 'Manage dropdown content like property types, amenities, regions and cities',
    },
  });

  await prisma.adminFeature.upsert({
    where: { key: 'AGENT_SUBSCRIPTIONS' },
    update: {
      label: 'Agent Subscriptions',
      description: 'Manage agent plans and approve agent subscription receipts',
    },
    create: {
      key: 'AGENT_SUBSCRIPTIONS',
      label: 'Agent Subscriptions',
      description: 'Manage agent plans and approve agent subscription receipts',
    },
  });

  await prisma.adminFeature.upsert({
    where: { key: 'BOOST_MANAGEMENT' },
    update: {
      label: 'Boosting',
      description: 'Manage boost pricing and approve featured listing receipt requests',
    },
    create: {
      key: 'BOOST_MANAGEMENT',
      label: 'Boosting',
      description: 'Manage boost pricing and approve featured listing receipt requests',
    },
  });
}

async function seedCatalogPropertyTypes() {
  const keys = ['APARTMENT', 'VILLA', 'TOWNHOUSE', 'PENTHOUSE', 'LAND', 'COMMERCIAL', 'STUDIO'] as const;

  const titleCase = (s: string) => s.charAt(0) + s.slice(1).toLowerCase();
  const labelFor = (key: string) => (key === 'STUDIO' ? 'Studio' : titleCase(key));

  // Note: The Prisma client may not have these model typings until prisma generate.
  // At runtime they exist once the schema is applied.
  await Promise.all(
    keys.map((key, idx) =>
      (prisma as any).catalogPropertyType.upsert({
        where: { key },
        update: { label: labelFor(key), enabled: true, sortOrder: idx },
        create: { key, label: labelFor(key), enabled: true, sortOrder: idx },
      }),
    ),
  );
}

async function main() {
  // SAFE SEED: does NOT delete users/properties.
  await seedAdminFeaturesContentManagement();
  await seedCatalogPropertyTypes();
  console.log('Catalog seed complete.');
}

main()
  .catch((e) => {
    console.error('Catalog seed failed', e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
