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

export const ESTIMATE_ROUTES = {
    INDEX: "/api/estimates",
    BY_ID: "/api/estimates/{id}",
} as const satisfies Record<string, ApiPath>

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

    // Note: GET /api/estimates/{id} is not in the OpenAPI schema.
    // This method uses a type cast and relies on the backend supporting the route.
    async getById(id: string) {
        const data = await this.get(ESTIMATE_ROUTES.INDEX, { query: { id } } as never)
        return {...data, data: (data as any)?.data?.[0] ?? null }
    }
}
