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

export const CREDIT_NOTE_ROUTES = {
    INDEX: "/api/credit-notes",
    BY_ID: "/api/credit-notes/{id}",
    ADD_ATTACHMENT: "/api/credit-notes/{id}/add-attachment",
    DELETE_ATTACHMENT: "/api/credit-notes/{id}/delete-attachment",
    ADD_INTERNAL_NOTE: "/api/credit-notes/{id}/add-internal-note",
    EDIT_INTERNAL_NOTE: "/api/credit-notes/{id}/edit-internal-note",
    DELETE_INTERNAL_NOTE: "/api/credit-notes/{id}/delete-internal-note",
} as const satisfies Record<string, ApiPath>

export class CreditNotesClient extends CrudClient<
    typeof CREDIT_NOTE_ROUTES.INDEX,
    typeof CREDIT_NOTE_ROUTES.BY_ID
> {
    constructor(baseUrl?: string, defaultOptions?: ApiClientOptions) {
        super(baseUrl, defaultOptions, CREDIT_NOTE_ROUTES.INDEX, CREDIT_NOTE_ROUTES.BY_ID)
    }

    async addAttachment(id: string, payload: FormData) {
        return this.post(CREDIT_NOTE_ROUTES.ADD_ATTACHMENT, payload as never, { params: { id } } as never)
    }

    async deleteAttachment(id: string, payload: ApiRequestBody<typeof CREDIT_NOTE_ROUTES.DELETE_ATTACHMENT, "delete">) {
        return this.delete(CREDIT_NOTE_ROUTES.DELETE_ATTACHMENT, { params: { id }, body: payload } as never)
    }

    async addInternalNote(id: string, payload: ApiRequestBody<typeof CREDIT_NOTE_ROUTES.ADD_INTERNAL_NOTE, "post">) {
        return this.post(CREDIT_NOTE_ROUTES.ADD_INTERNAL_NOTE, payload, { params: { id } } as never)
    }

    async editInternalNote(id: string, payload: ApiRequestBody<typeof CREDIT_NOTE_ROUTES.EDIT_INTERNAL_NOTE, "delete">) {
        return this.delete(CREDIT_NOTE_ROUTES.EDIT_INTERNAL_NOTE, { params: { id }, body: payload } as never)
    }

    async deleteInternalNote(id: string, payload: ApiRequestBody<typeof CREDIT_NOTE_ROUTES.DELETE_INTERNAL_NOTE, "delete">) {
        return this.delete(CREDIT_NOTE_ROUTES.DELETE_INTERNAL_NOTE, { params: { id }, body: payload } as never)
    }
}
