import { ApiClient, type ApiClientOptions } from "../infra/client"

export const INSPECTION_TEMPLATE_ROUTES = {
    INDEX: "/api/inspection-templates",
    BY_ID: "/api/inspection-templates/{id}",
    DUPLICATE: "/api/inspection-templates/{id}/duplicate",
    SECTIONS: "/api/inspection-templates/{template}/sections",
    SECTION_BY_ID: "/api/inspection-templates/{template}/sections/{section}",
    CHECKPOINTS: "/api/inspection-templates/{template}/sections/{section}/checkpoints",
    CHECKPOINT_BY_ID: "/api/inspection-templates/{template}/sections/{section}/checkpoints/{id}",
} as const

export type InspectionTemplateCheckPointRecordType =
    | "record_conditions"
    | "record_audio"
    | "record_video"
    | "capture_photo"
    | "wire_frame"
    | "none"

export type InspectionSeverity = "good" | "attention" | "critical" | "na" | "not_inspected"

export type InspectionTemplateCheckPoint = {
    id: number
    section_id: number
    name: string
    description?: string | null
    record_type: InspectionTemplateCheckPointRecordType
    severity_default: InspectionSeverity
    recommended_service_id?: number | null
    recommended_part_id?: number | null
    sort_order: number
}

export type InspectionTemplateSection = {
    id: number
    template_id: number
    name: string
    sort_order: number
    icon?: string | null
    color?: string | null
    check_points?: InspectionTemplateCheckPoint[]
}

export type InspectionTemplate = {
    id: number
    name: string
    description?: string | null
    shop_type_id?: number | null
    vehicle_type?: string | null
    is_active: boolean
    sections?: InspectionTemplateSection[]
}

export type InspectionTemplateCreatePayload = {
    name: string
    description?: string | null
    shop_type_id?: number | null
    vehicle_type?: string | null
    is_active?: boolean
}

export type InspectionTemplateUpdatePayload = Partial<InspectionTemplateCreatePayload>

export type InspectionTemplateSectionPayload = {
    name: string
    sort_order?: number
    icon?: string | null
    color?: string | null
}

export type InspectionTemplateCheckPointPayload = {
    name: string
    description?: string | null
    record_type?: InspectionTemplateCheckPointRecordType
    severity_default?: InspectionSeverity
    recommended_service_id?: number | null
    recommended_part_id?: number | null
    sort_order?: number
}

type ListResponse<T> = {
    data: T[]
    meta?: {
        current_page: number
        last_page: number
        per_page: number
        total: number
    }
}

type ItemResponse<T> = { data: T; message?: string }

export class InspectionTemplatesClient extends ApiClient {
    constructor(baseUrl?: string, defaultOptions?: ApiClientOptions) {
        super(baseUrl, defaultOptions)
    }

    async list(query?: Record<string, string | number | boolean>): Promise<ListResponse<InspectionTemplate>> {
        return this.get(INSPECTION_TEMPLATE_ROUTES.INDEX as never, { query } as never) as never
    }

    async show(id: number | string): Promise<ItemResponse<InspectionTemplate>> {
        return this.get(INSPECTION_TEMPLATE_ROUTES.BY_ID as never, { params: { id: String(id) } } as never) as never
    }

    async create(payload: InspectionTemplateCreatePayload): Promise<ItemResponse<InspectionTemplate>> {
        return this.post(INSPECTION_TEMPLATE_ROUTES.INDEX as never, payload as never) as never
    }

    async update(id: number | string, payload: InspectionTemplateUpdatePayload): Promise<ItemResponse<InspectionTemplate>> {
        return this.put(INSPECTION_TEMPLATE_ROUTES.BY_ID as never, payload as never, { params: { id: String(id) } } as never) as never
    }

    async destroy(id: number | string): Promise<{ message: string }> {
        return this.delete(INSPECTION_TEMPLATE_ROUTES.BY_ID as never, { params: { id: String(id) } } as never) as never
    }

    async duplicate(id: number | string): Promise<ItemResponse<InspectionTemplate>> {
        return this.post(INSPECTION_TEMPLATE_ROUTES.DUPLICATE as never, undefined as never, { params: { id: String(id) } } as never) as never
    }

    // ── Sections ──
    async listSections(templateId: number | string): Promise<ListResponse<InspectionTemplateSection>> {
        return this.get(INSPECTION_TEMPLATE_ROUTES.SECTIONS as never, { params: { template: String(templateId) } } as never) as never
    }

    async createSection(templateId: number | string, payload: InspectionTemplateSectionPayload): Promise<ItemResponse<InspectionTemplateSection>> {
        return this.post(INSPECTION_TEMPLATE_ROUTES.SECTIONS as never, payload as never, { params: { template: String(templateId) } } as never) as never
    }

    async updateSection(templateId: number | string, sectionId: number | string, payload: Partial<InspectionTemplateSectionPayload>): Promise<ItemResponse<InspectionTemplateSection>> {
        return this.put(INSPECTION_TEMPLATE_ROUTES.SECTION_BY_ID as never, payload as never, { params: { template: String(templateId), section: String(sectionId) } } as never) as never
    }

    async destroySection(templateId: number | string, sectionId: number | string): Promise<{ message: string }> {
        return this.delete(INSPECTION_TEMPLATE_ROUTES.SECTION_BY_ID as never, { params: { template: String(templateId), section: String(sectionId) } } as never) as never
    }

    // ── Checkpoints ──
    async listCheckpoints(templateId: number | string, sectionId: number | string): Promise<ListResponse<InspectionTemplateCheckPoint>> {
        return this.get(INSPECTION_TEMPLATE_ROUTES.CHECKPOINTS as never, { params: { template: String(templateId), section: String(sectionId) } } as never) as never
    }

    async createCheckpoint(templateId: number | string, sectionId: number | string, payload: InspectionTemplateCheckPointPayload): Promise<ItemResponse<InspectionTemplateCheckPoint>> {
        return this.post(INSPECTION_TEMPLATE_ROUTES.CHECKPOINTS as never, payload as never, { params: { template: String(templateId), section: String(sectionId) } } as never) as never
    }

    async updateCheckpoint(templateId: number | string, sectionId: number | string, id: number | string, payload: Partial<InspectionTemplateCheckPointPayload>): Promise<ItemResponse<InspectionTemplateCheckPoint>> {
        return this.put(INSPECTION_TEMPLATE_ROUTES.CHECKPOINT_BY_ID as never, payload as never, { params: { template: String(templateId), section: String(sectionId), id: String(id) } } as never) as never
    }

    async destroyCheckpoint(templateId: number | string, sectionId: number | string, id: number | string): Promise<{ message: string }> {
        return this.delete(INSPECTION_TEMPLATE_ROUTES.CHECKPOINT_BY_ID as never, { params: { template: String(templateId), section: String(sectionId), id: String(id) } } as never) as never
    }
}
