import {
  ClaimStatusSchema,
  ClaimUncheckedCreateInputObjectSchema,
} from "@repo/db/usedSchemas";
import { z } from "zod";
import { Decimal } from "decimal.js";
import { Staff } from "@repo/db/types";
import { makeEnumOptions } from "../utils";

export const insertClaimSchema = (
  ClaimUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
).omit({
  id: true,
  createdAt: true,
  updatedAt: true,
});
export type InsertClaim = z.infer<typeof insertClaimSchema>;

export const updateClaimSchema = (
  ClaimUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
)
  .omit({
    id: true,
    createdAt: true,
    updatedAt: true,
  })
  .partial();

export type UpdateClaim = z.infer<typeof updateClaimSchema>;

// Extend the schema to inject `userId` manually (since it's not passed by the client)
export const ExtendedClaimSchema = (
  ClaimUncheckedCreateInputObjectSchema as unknown as z.ZodObject<any>
).extend({
  userId: z.number(),
});

export type Claim = z.infer<typeof ClaimUncheckedCreateInputObjectSchema>;
export type ClaimStatus = z.infer<typeof ClaimStatusSchema>;
export const claimStatusOptions =
  makeEnumOptions<
    typeof ClaimStatusSchema extends z.ZodTypeAny
      ? z.infer<typeof ClaimStatusSchema>
      : string
  >(ClaimStatusSchema);
export type ClaimStatusOptions =
  (typeof claimStatusOptions)[keyof typeof claimStatusOptions];

export type ClaimFileMeta = {
  id?: number;
  filename: string;
  mimeType?: string | null;
};

//used in claim-form
export interface InputServiceLine {
  procedureCode: string;
  procedureDate: string; // YYYY-MM-DD
  oralCavityArea?: string;
  toothNumber?: string;
  toothSurface?: string;
  totalBilled: Decimal;
  totalPaid?: Decimal;
  totalAdjusted?: Decimal;
}

// Claim model with embedded service lines
export type ClaimWithServiceLines = Claim & {
  serviceLines: {
    id: number;
    claimId: number | null;
    procedureCode: string;
    procedureDate: Date;
    oralCavityArea: string | null;
    toothNumber: string | null;
    toothSurface: string | null;
    totalBilled: Decimal;
    totalPaid: Decimal;
    totalAdjusted: Decimal;
    status: string;
  }[];
  staff?: Staff | null;
  claimFiles?: ClaimFileMeta[] | null;
};

export type MissingTeethStatus = "No_missing" | "endentulous" | "Yes_missing";

export interface ClaimFormData {
  patientId: number;
  appointmentId: number;
  userId: number;
  staffId: number;
  patientName: string;
  memberId: string;
  dateOfBirth: string;
  remarks: string;
  missingTeethStatus: MissingTeethStatus;
  missingTeeth: Record<string, "X" | "O">; // keys: T_1..T_32, T_A..T_T
  serviceDate: string; // YYYY-MM-DD
  insuranceProvider: string;
  insuranceSiteKey?: string;
  status: string; // default "pending"
  serviceLines: InputServiceLine[];
  claimId?: number;
  claimFiles?: ClaimFileMeta[];
}

export interface ClaimPreAuthData {
  patientId: number;
  userId: number;
  staffId: number;
  patientName: string;
  memberId: string;
  dateOfBirth: string;
  remarks: string;
  missingTeethStatus: MissingTeethStatus;
  missingTeeth: Record<string, "X" | "O">; // keys: T_1..T_32, T_A..T_T
  serviceDate: string; // YYYY-MM-DD
  insuranceProvider: string;
  insuranceSiteKey?: string;
  status: string; // default "pending"
  serviceLines: InputServiceLine[];
  claimFiles?: ClaimFileMeta[];
}
