import {
  sqliteTable,
  text,
  integer,
  uniqueIndex,
} from "drizzle-orm/sqlite-core";
import { sql } from "drizzle-orm";

// ==================== BRANCHES ====================
export const branches = sqliteTable("branches", {
  id: text("id").primaryKey(),
  name: text("name").notNull(),
  code: text("code").notNull().unique(),
  createdAt: integer("created_at", { mode: "timestamp" })
    .notNull()
    .default(sql`(unixepoch())`),
});

// ==================== EMPLOYEES ====================
export const employees = sqliteTable("employees", {
  id: text("id").primaryKey(),
  employeeId: text("employee_id").notNull().unique(),
  name: text("name").notNull(),
  branchId: text("branch_id")
    .notNull()
    .references(() => branches.id),
  isAdmin: integer("is_admin", { mode: "boolean" }).notNull().default(false),
  pin: text("pin").notNull(),
  isActive: integer("is_active", { mode: "boolean" }).notNull().default(true),
  createdAt: integer("created_at", { mode: "timestamp" })
    .notNull()
    .default(sql`(unixepoch())`),
});

// ==================== CANDIDATES ====================
export const candidates = sqliteTable("candidates", {
  id: text("id").primaryKey(),
  employeeId: text("employee_id")
    .notNull()
    .references(() => employees.id),
  branchId: text("branch_id")
    .notNull()
    .references(() => branches.id),
  position: text("position"),
  statement: text("statement"),
  photoUrl: text("photo_url"),
  isApproved: integer("is_approved", { mode: "boolean" })
    .notNull()
    .default(true),
  createdAt: integer("created_at", { mode: "timestamp" })
    .notNull()
    .default(sql`(unixepoch())`),
});

// ==================== VOTES (ANONYMOUS - no voter info) ====================
export const votes = sqliteTable("votes", {
  id: text("id").primaryKey(),
  candidateId: text("candidate_id")
    .notNull()
    .references(() => candidates.id),
  branchId: text("branch_id")
    .notNull()
    .references(() => branches.id),
  votedAt: integer("voted_at", { mode: "timestamp" })
    .notNull()
    .default(sql`(unixepoch())`),
});

// ==================== VOTE RECORDS (tracks who voted, NOT who they voted for) ====================
export const voteRecords = sqliteTable("vote_records", {
  id: text("id").primaryKey(),
  employeeId: text("employee_id")
    .notNull()
    .references(() => employees.id)
    .unique(),
  voteCount: integer("vote_count").notNull().default(0),
  updatedAt: integer("updated_at", { mode: "timestamp" })
    .notNull()
    .default(sql`(unixepoch())`),
});

// ==================== SETTINGS ====================
export const settings = sqliteTable("settings", {
  id: text("id").primaryKey(),
  key: text("key").notNull().unique(),
  value: text("value").notNull(),
  updatedAt: integer("updated_at", { mode: "timestamp" })
    .notNull()
    .default(sql`(unixepoch())`),
});

// ==================== RELATIONS ====================
import { relations } from "drizzle-orm";

export const branchesRelations = relations(branches, ({ many }) => ({
  employees: many(employees),
  candidates: many(candidates),
  votes: many(votes),
}));

export const employeesRelations = relations(employees, ({ one }) => ({
  branch: one(branches, {
    fields: [employees.branchId],
    references: [branches.id],
  }),
}));

export const candidatesRelations = relations(candidates, ({ one }) => ({
  employee: one(employees, {
    fields: [candidates.employeeId],
    references: [employees.id],
  }),
  branch: one(branches, {
    fields: [candidates.branchId],
    references: [branches.id],
  }),
}));

export const votesRelations = relations(votes, ({ one }) => ({
  candidate: one(candidates, {
    fields: [votes.candidateId],
    references: [candidates.id],
  }),
  branch: one(branches, {
    fields: [votes.branchId],
    references: [branches.id],
  }),
}));
