import { departments, positions } from '@/db/schema';
import { db } from '@/lib/db';

const DEFAULT_DEPARTMENTS = [
  {
    code: 'HR',
    name: 'Human Resources',
    description: 'People operations, recruitment, and employee relations.'
  },
  {
    code: 'QA',
    name: 'Quality Assurance',
    description: 'Quality systems, audits, and compliance oversight.'
  },
  {
    code: 'PRD',
    name: 'Production',
    description: 'Manufacturing, operations, and production planning.'
  },
  {
    code: 'ENG',
    name: 'Engineering',
    description: 'Technical development, maintenance, and process improvement.'
  },
  {
    code: 'LOG',
    name: 'Logistics',
    description: 'Warehouse, shipping, receiving, and inventory coordination.'
  },
  {
    code: 'FIN',
    name: 'Finance',
    description: 'Accounting, budgeting, and financial control.'
  }
] as const;

const DEFAULT_POSITIONS = [
  {
    code: 'MGR',
    name: 'Manager',
    description: 'Leads team execution, planning, and performance.'
  },
  {
    code: 'SUP',
    name: 'Supervisor',
    description: 'Oversees day-to-day operations and frontline staff.'
  },
  {
    code: 'SNR',
    name: 'Senior Staff',
    description: 'Experienced individual contributor with advanced responsibility.'
  },
  {
    code: 'STF',
    name: 'Staff',
    description: 'Core contributor responsible for standard operational work.'
  },
  {
    code: 'JNR',
    name: 'Junior Staff',
    description: 'Entry-level contributor working under guidance.'
  },
  {
    code: 'TRN',
    name: 'Trainee',
    description: 'Newly onboarded employee in structured training period.'
  }
] as const;

export async function seedOrganizationMasterData(organizationId: string) {
  await db
    .insert(departments)
    .values(
      DEFAULT_DEPARTMENTS.map((department) => ({
        organizationId,
        code: department.code,
        name: department.name,
        description: department.description
      }))
    )
    .onConflictDoNothing({
      target: [departments.organizationId, departments.name]
    });

  await db
    .insert(positions)
    .values(
      DEFAULT_POSITIONS.map((position) => ({
        organizationId,
        code: position.code,
        name: position.name,
        description: position.description
      }))
    )
    .onConflictDoNothing({
      target: [positions.organizationId, positions.name]
    });
}

export { DEFAULT_DEPARTMENTS, DEFAULT_POSITIONS };
