import { CrudClient } from "../infra/crud-client"
import type { ApiClientOptions } from "../infra/client"
import type { ApiPath } from "../infra/types"
import type { ApiListQueryParams } from "../contracts/types"

export const CUSTOMER_ROUTES = {
    INDEX: "/api/customers",
    BY_ID: "/api/customers/{id}",
    EXPORT: "/api/customers/export",
    IMPORT: "/api/customers/import",
    IMPORT_SAMPLE: "/api/customers/import/sample",
    CUSTOMER_TYPES: "/api/customer-types",
    NOTES: "/api/customers/{id}/notes/{note_id}",
} as const satisfies Record<string, ApiPath>

export class CustomersClient extends CrudClient<typeof CUSTOMER_ROUTES.INDEX, typeof CUSTOMER_ROUTES.BY_ID> {

    constructor(baseUrl?: string, defaultOptions?: ApiClientOptions) {
        super(
            baseUrl,
            defaultOptions,
            CUSTOMER_ROUTES.INDEX,
            CUSTOMER_ROUTES.BY_ID,
            CUSTOMER_ROUTES.IMPORT,
            CUSTOMER_ROUTES.EXPORT,
            "GET",
        )
    }

    async getById(id: string) {
        return this.show(id)
    }

    async listCustomerTypes(query?: ApiListQueryParams) {
        return this.get(CUSTOMER_ROUTES.CUSTOMER_TYPES, query ? { query } as never : undefined)
    }

    async downloadImportSample() {
        return this.fetchBlob(CUSTOMER_ROUTES.IMPORT_SAMPLE, { method: "GET" })
    }

    async addNote(id: string, payload: { note: string }) {
        return this.post(CUSTOMER_ROUTES.NOTES, payload as never, { params: { id, note_id: "0" } } as never)
    }

    async deleteNote(customerId: string, noteId: number) {
        return this.delete(CUSTOMER_ROUTES.NOTES, { params: { id: customerId, note_id: String(noteId) } } as never)
    }
}
