// Mock database implementation - in production, this would connect to a real database

const vouchers: Voucher[] = [
  {
    id: "1",
    serialNumber: "FU2024001",
    pin: "1234",
    price: 5000,
    used: false,
    purchaseDate: "2024-01-10",
    email: "john.doe@email.com",
  },
  {
    id: "2",
    serialNumber: "FU2024002",
    pin: "5678",
    price: 5000,
    used: true,
    purchaseDate: "2024-01-12",
    email: "jane.smith@email.com",
    userId: "user_123",
  },
]

const applications: StudentApplication[] = [
  {
    id: "app_1",
    userId: "user_123",
    voucherSerialNumber: "FU2024002",
    submitted: false,
    biodata: {
      firstName: "Jane",
      lastName: "Smith",
      dateOfBirth: "1995-03-15",
      gender: "female",
      nationality: "Nigerian",
      stateOfOrigin: "Lagos",
      phoneNumber: "+234 801 234 5678",
      email: "jane.smith@email.com",
    },
    address: {
      permanentAddress: "123 Main Street",
      permanentCity: "Lagos",
      permanentState: "Lagos",
      permanentCountry: "Nigeria",
      sameAsPermanent: true,
    },
    education: {
      entries: [],
    },
    program: {
      firstChoice: "Computer Science",
      studyMode: "full-time",
      entryLevel: "100",
      personalStatement: "",
    },
    guardian: {
      guardianName: "",
      relationship: "",
      phoneNumber: "",
      address: "",
      emergencyName: "",
      emergencyPhone: "",
    },
    scholarship: {
      applyingForScholarship: false,
    },
    referee: {
      referees: [],
    },
    uploads: {
      files: [],
    },
    declaration: {
      truthfulnessDeclaration: false,
      accuracyDeclaration: false,
      consequencesDeclaration: false,
      updatesDeclaration: false,
      termsDeclaration: false,
      signature: "",
      date: "",
    },
  },
]

// Voucher operations
export const voucherDb = {
  async findBySerialAndPin(serialNumber: string, pin: string): Promise<Voucher | null> {
    return vouchers.find((v) => v.serialNumber === serialNumber && v.pin === pin) || null
  },

  async markAsUsed(serialNumber: string, userId: string): Promise<boolean> {
    const voucher = vouchers.find((v) => v.serialNumber === serialNumber)
    if (voucher) {
      voucher.used = true
      voucher.userId = userId
      return true
    }
    return false
  },

  async create(voucherData: Omit<Voucher, "id">): Promise<Voucher> {
    const voucher: Voucher = {
      id: Date.now().toString(),
      ...voucherData,
    }
    vouchers.push(voucher)
    return voucher
  },
}

// Application operations
export const applicationDb = {
  async findByUserId(userId: string): Promise<StudentApplication | null> {
    return applications.find((app) => app.userId === userId) || null
  },

  async create(applicationData: Omit<StudentApplication, "id">): Promise<StudentApplication> {
    const application: StudentApplication = {
      id: `app_${Date.now()}`,
      ...applicationData,
    }
    applications.push(application)
    return application
  },

  async update(userId: string, updates: Partial<StudentApplication>): Promise<StudentApplication | null> {
    const index = applications.findIndex((app) => app.userId === userId)
    if (index !== -1) {
      applications[index] = { ...applications[index], ...updates }
      return applications[index]
    }
    return null
  },

  async submit(userId: string): Promise<boolean> {
    const application = applications.find((app) => app.userId === userId)
    if (application) {
      application.submitted = true
      application.submittedAt = new Date().toISOString()
      return true
    }
    return false
  },
}

import type { Voucher, StudentApplication } from "./types"
