export type {
  attendance,
  attendance_project_logs,
  audit_logs,
  bank_accounts,
  company_settings,
  customers,
  invoice_items,
  invoices,
  item_templates,
  leave_balances,
  leave_requests,
  number_sequences,
  order_items,
  order_sections,
  orders,
  permissions,
  project_notes,
  projects,
  quotation_items,
  quotations,
  received_invoices,
  refresh_tokens,
  role_permissions,
  roles,
  scope_sections,
  scope_template_sections,
  scope_templates,
  totp_login_tokens,
  trips,
  users,
  vehicles,
} from "@prisma/client";

export {
  attendance_leave_type,
  leave_requests_leave_type,
  leave_requests_status,
  received_invoices_status,
} from "@prisma/client";

// --- Auth types ---

export interface AuthData {
  userId: number;
  username: string;
  email: string;
  firstName: string;
  lastName: string;
  roleId: number | null;
  roleName: string | null;
  permissions: string[];
  totp_enabled: boolean;
  require_2fa?: boolean;
}

export interface JwtPayload {
  sub: number;
  username: string;
  // NOTE: the JWT carries `role`, but the loaded `AuthData` uses `roleName`.
  // Admin checks MUST use `authData.roleName === "admin"` — `authData.role`
  // does not exist. Keep these names distinct deliberately; do not "unify".
  role: string | null;
  iat: number;
  exp: number;
}

export interface LoginRequest {
  username: string;
  password: string;
  remember_me?: boolean;
}

export interface TotpVerifyRequest {
  login_token: string;
  totp_code: string;
}

// --- API response types ---

export interface ApiResponse<T = unknown> {
  success: boolean;
  data?: T;
  error?: string;
  pagination?: PaginationMeta;
}

export interface PaginationMeta {
  page: number;
  limit: number;
  per_page: number;
  total: number;
  total_pages: number;
}

export interface PaginationQuery {
  page?: number;
  limit?: number;
  sort?: string;
  order?: "asc" | "desc";
  search?: string;
}

// --- Audit log types ---

export type AuditAction =
  | "login"
  | "login_totp"
  | "login_backup"
  | "logout"
  | "login_failed"
  | "create"
  | "update"
  | "delete"
  | "activate"
  | "deactivate"
  | "confirm"
  | "cancel"
  | "upload"
  | "password_change"
  | "permission_change"
  | "access_denied";

export type EntityType =
  | "user"
  | "role"
  | "attendance"
  | "leave_request"
  | "invoice"
  | "quotation"
  | "order"
  | "issued_order"
  | "project"
  | "customer"
  | "trip"
  | "vehicle"
  | "bank_account"
  | "company_settings"
  | "leave_balance"
  | "project_file"
  | "audit_logs"
  | "warehouse_item"
  | "warehouse_receipt"
  | "warehouse_issue"
  | "warehouse_reservation"
  | "warehouse_inventory"
  | "warehouse_supplier"
  | "warehouse_category"
  | "warehouse_location"
  | "warehouse_receipt_attachment"
  | "work_plan_entry"
  | "work_plan_override"
  | "plan_category"
  | "session";
