import { 
  seedUnits, 
  seedStaffRoles, 
  seedStaffPermissions, 
  seedRolePermissions, 
  seedKeyValStore,
  type UnitSeedData,
  type RolePermissionAssignment,
  type KeyValSeedData,
  type StaffRoleName,
  type StaffPermissionName,
} from '@/src/dbService'
import { getMinOrderValue, getDeliveryCharge } from '@/src/lib/env-exporter'
import { CONST_KEYS } from '@/src/lib/const-keys'

export async function seed() {
  console.log("Seeding database...");

  // Seed units
  const unitsToSeed: UnitSeedData[] = [
    { shortNotation: "Kg", fullName: "Kilogram" },
    { shortNotation: "L", fullName: "Litre" },
    { shortNotation: "Dz", fullName: "Dozen" },
    { shortNotation: "Pc", fullName: "Unit Piece" },
  ];
  await seedUnits(unitsToSeed);

  // Seed staff roles
  const rolesToSeed: StaffRoleName[] = ['super_admin', 'admin', 'marketer', 'delivery_staff'];
  await seedStaffRoles(rolesToSeed);

  // Seed staff permissions
  const permissionsToSeed: StaffPermissionName[] = ['crud_product', 'make_coupon', 'crud_staff_users'];
  await seedStaffPermissions(permissionsToSeed);

  // Seed role-permission assignments
  const rolePermissionAssignments: RolePermissionAssignment[] = [
    // super_admin gets all permissions
    { roleName: 'super_admin', permissionName: 'crud_product' },
    { roleName: 'super_admin', permissionName: 'make_coupon' },
    { roleName: 'super_admin', permissionName: 'crud_staff_users' },
    // admin gets product and coupon permissions
    { roleName: 'admin', permissionName: 'crud_product' },
    { roleName: 'admin', permissionName: 'make_coupon' },
    // marketer gets coupon permission
    { roleName: 'marketer', permissionName: 'make_coupon' },
  ];
  await seedRolePermissions(rolePermissionAssignments);

  // Seed key-val store constants
  const constantsToSeed: KeyValSeedData[] = [
    { key: CONST_KEYS.readableOrderId, value: 0 },
    { key: CONST_KEYS.minRegularOrderValue, value: getMinOrderValue() },
    { key: CONST_KEYS.freeDeliveryThreshold, value: getMinOrderValue() },
    { key: CONST_KEYS.deliveryCharge, value: getDeliveryCharge() },
    { key: CONST_KEYS.flashFreeDeliveryThreshold, value: 500 },
    { key: CONST_KEYS.flashDeliveryCharge, value: 69 },
    { key: CONST_KEYS.popularItems, value: [] },
    { key: CONST_KEYS.allItemsOrder, value: [] },
    { key: CONST_KEYS.versionNum, value: '1.1.0' },
    { key: CONST_KEYS.playStoreUrl, value: 'https://play.google.com/store/apps/details?id=in.freshyo.app' },
    { key: CONST_KEYS.appStoreUrl, value: 'https://apps.apple.com/in/app/freshyo/id6756889077' },
    { key: CONST_KEYS.isFlashDeliveryEnabled, value: false },
    { key: CONST_KEYS.supportMobile, value: '8688182552' },
    { key: CONST_KEYS.supportEmail, value: 'qushammohd@gmail.com' },
  ];
  await seedKeyValStore(constantsToSeed);

  console.log("Seeding completed.");
}
