import { Database } from 'bun:sqlite';
import { drizzle } from 'drizzle-orm/bun-sqlite';
import * as schema from './schema';
import path from 'path';

const dbPath = path.join(process.cwd(), 'db', 'voting.db');

const sqlite = new Database(dbPath);
sqlite.run('PRAGMA journal_mode = WAL');
sqlite.run('PRAGMA foreign_keys = ON');

const db = drizzle(sqlite, { schema });

import { branches, employees, candidates, settings } from './schema';
import crypto from 'crypto';

function hashPin(pin: string): string {
  return crypto.createHash('sha256').update(pin).digest('hex');
}

function generateId(): string {
  return crypto.randomUUID();
}

async function seed() {
  console.log('🌱 Seeding database...');

  // Check if already seeded
  const existingBranches = await db.select().from(branches).limit(1);
  if (existingBranches.length > 0) {
    console.log('⏭️ Database already seeded, skipping...');
    return;
  }

  // ===== BRANCHES =====
  const branchData = [
    { id: generateId(), name: 'สำนักงานใหญ่ (กลาง)', code: 'HQ' },
    { id: generateId(), name: 'สาขาเชียงใหม่', code: 'CNX' },
    { id: generateId(), name: 'สาขาขอนแก่น', code: 'KKC' },
    { id: generateId(), name: 'สาขาภูเก็ต', code: 'PKT' },
    { id: generateId(), name: 'สาขานนทบุรี', code: 'NBI' },
    { id: generateId(), name: 'สาขาชลบุรี', code: 'CBI' },
  ];

  for (const branch of branchData) {
    await db.insert(branches).values(branch);
  }
  console.log(`✅ Created ${branchData.length} branches`);

  const hqBranch = branchData[0];
  const cnxBranch = branchData[1];
  const kkcBranch = branchData[2];
  const pktBranch = branchData[3];
  const nbiBranch = branchData[4];
  const cbiBranch = branchData[5];

  // ===== EMPLOYEES =====
  const employeeData = [
    { id: generateId(), employeeId: 'ADMIN001', name: 'ผู้ดูแลระบบ', branchId: hqBranch.id, isAdmin: true, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP001', name: 'สมชาย ใจดี', branchId: hqBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP002', name: 'สมหญิง รักเรียน', branchId: hqBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP003', name: 'วิชัย มุ่งมั่น', branchId: hqBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP004', name: 'พรทิพย์ สว่างจิต', branchId: hqBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP005', name: 'ธนกร เจริญสุข', branchId: hqBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP006', name: 'รัตนา ผดุงศรี', branchId: hqBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP007', name: 'กิตติพงษ์ วงศ์ทอง', branchId: hqBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP008', name: 'นภัสสร ศรีสุข', branchId: hqBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP009', name: 'ประเสริฐ กิจสุข', branchId: hqBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP010', name: 'อรุณี แสงจันทร์', branchId: hqBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP011', name: 'เกรียงศักดิ์ พงษ์ประเสริฐ', branchId: cnxBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP012', name: 'ศิริลักษณ์ ดอกไม้', branchId: cnxBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP013', name: 'ภาณุพงศ์ นคร', branchId: cnxBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP014', name: 'จิราภรณ์ ศรีสมบูรณ์', branchId: cnxBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP015', name: 'อดิศร ภูมิพัฒน์', branchId: cnxBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP016', name: 'วิภาดา เรืองศรี', branchId: cnxBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP017', name: 'ชาตรี แสนสุข', branchId: cnxBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP018', name: 'พิมพ์ใจ รุ่งเรือง', branchId: cnxBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP019', name: 'สุรชัย พลเมือง', branchId: kkcBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP020', name: 'วรรณา สิทธิผล', branchId: kkcBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP021', name: 'ธีรพงษ์ จันทร์เพ็ญ', branchId: kkcBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP022', name: 'ลลิตา สุขสันต์', branchId: kkcBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP023', name: 'บุญมี ศรีเมือง', branchId: kkcBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP024', name: 'จิตรา ทองดี', branchId: kkcBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP025', name: 'วิชัย ทะเลใส', branchId: pktBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP026', name: 'สมศรี หาดทอง', branchId: pktBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP027', name: 'ประภาส แก้วมุก', branchId: pktBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP028', name: 'นิตยา สมุทร', branchId: pktBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP029', name: 'กมลชนก วิไลลักษณ์', branchId: pktBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP030', name: 'พีรพงศ์ ชลธี', branchId: pktBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP031', name: 'ธนวัฒน์ บุญเรือง', branchId: nbiBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP032', name: 'สุนีย์ รุ่งอรุณ', branchId: nbiBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP033', name: 'ชัยวัฒน์ ประสิทธิ์', branchId: nbiBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP034', name: 'วิมล จันทร์แก้ว', branchId: nbiBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP035', name: 'สมบัติ ทองคำ', branchId: nbiBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP036', name: 'วีรชัย ศรีสมุทร', branchId: cbiBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP037', name: 'จันทร์เพ็ญ แสงทอง', branchId: cbiBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP038', name: 'อนันต์ ชัยชนะ', branchId: cbiBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP039', name: 'สายฝน พรมเย็น', branchId: cbiBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
    { id: generateId(), employeeId: 'EMP040', name: 'ชูศรี บุญมาก', branchId: cbiBranch.id, isAdmin: false, pin: hashPin('1234'), isActive: true },
  ];

  for (const emp of employeeData) {
    await db.insert(employees).values(emp);
  }
  console.log(`✅ Created ${employeeData.length} employees`);

  // ===== CANDIDATES =====
  const candidateEmployees = employeeData.filter(e => !e.isAdmin && [
    'EMP001', 'EMP003', 'EMP005', 'EMP007', 'EMP009',
    'EMP011', 'EMP013', 'EMP015', 'EMP017',
    'EMP019', 'EMP021', 'EMP023',
    'EMP025', 'EMP027', 'EMP029',
    'EMP031', 'EMP033',
    'EMP036', 'EMP038',
  ].includes(e.employeeId));

  const candidateData = candidateEmployees.map(emp => ({
    id: generateId(),
    employeeId: emp.id,
    branchId: emp.branchId,
    position: 'พนักงาน',
    statement: 'พร้อมที่จะเป็นตัวแทนพนักงาน เพื่อส่งเสริมสิทธิและคุณภาพชีวิตในการทำงาน',
    isApproved: true,
  }));

  for (const cand of candidateData) {
    await db.insert(candidates).values(cand);
  }
  console.log(`✅ Created ${candidateData.length} candidates`);

  // ===== SETTINGS =====
  const now = new Date();
  const votingStart = new Date(now.getTime() - 1 * 24 * 60 * 60 * 1000);
  const votingEnd = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000);

  const settingsData = [
    { id: generateId(), key: 'maxVotesPerPerson', value: '5' },
    { id: generateId(), key: 'votingStartDate', value: votingStart.toISOString() },
    { id: generateId(), key: 'votingEndDate', value: votingEnd.toISOString() },
    { id: generateId(), key: 'votingEnabled', value: 'true' },
    { id: generateId(), key: 'resultsVisible', value: 'false' },
    { id: generateId(), key: 'systemName', value: 'ระบบเลือกตั้งตัวแทนพนักงาน' },
  ];

  for (const setting of settingsData) {
    await db.insert(settings).values(setting);
  }
  console.log(`✅ Created ${settingsData.length} settings`);

  console.log('🎉 Seeding complete!');
  console.log('');
  console.log('📋 Login credentials:');
  console.log('   Admin: ADMIN001 / 1234');
  console.log('   Employee: EMP001-EMP040 / 1234');
}

seed().catch(console.error);
