import { api } from '$lib/api';
import type { ApiResponse } from '$lib/api';

export enum PortType {
    ENTRY = 'ENTRY',
    EXIT = 'EXIT',
    BOTH = 'BOTH'
}

export interface Port {
    id: number;
    port_code: string;
    description: string | null;
    location_code: string;
    location_description: string | null;
    port_type: PortType;
    created_at: string | null;
    updated_at: string | null;
}

export interface PortCreate {
    port_code: string;
    description?: string | null;
    location_code: string;
    location_description?: string | null;
    port_type?: PortType;
}

export interface PortUpdate {
    port_code?: string;
    description?: string | null;
    location_code?: string;
    location_description?: string | null;
    port_type?: PortType;
}

export interface PortListResponse {
    items: Port[];
    total: number;
    page: number;
    page_size: number;
    pages: number;
}

class PortsApi {
    private baseUrl = '/v1/a76/ports';

    async list(
        companyId: string | number,
        params?: Record<string, any>
    ): Promise<ApiResponse<PortListResponse>> {
        const queryParams = new URLSearchParams({
            company_id: companyId.toString(),
            ...params
        });
        return api.get<PortListResponse>(`${this.baseUrl}/?${queryParams.toString()}`);
    }

    async get(id: string | number, companyId: string | number): Promise<ApiResponse<Port>> {
        const queryParams = new URLSearchParams({
            company_id: companyId.toString()
        });
        return api.get<Port>(`${this.baseUrl}/${id}?${queryParams.toString()}`);
    }

    async create(data: PortCreate, companyId: string | number): Promise<ApiResponse<Port>> {
        const queryParams = new URLSearchParams({
            company_id: companyId.toString()
        });
        return api.post<Port>(`${this.baseUrl}/?${queryParams.toString()}`, data);
    }

    async update(id: string | number, data: PortUpdate, companyId: string | number): Promise<ApiResponse<Port>> {
        const queryParams = new URLSearchParams({
            company_id: companyId.toString()
        });
        return api.put<Port>(`${this.baseUrl}/${id}/?${queryParams.toString()}`, data);
    }

    async delete(id: string | number, companyId: string | number): Promise<ApiResponse<void>> {
        const queryParams = new URLSearchParams({
            company_id: companyId.toString()
        });
        return api.delete(`${this.baseUrl}/${id}?${queryParams.toString()}`);
    }
}

export const portsApi = new PortsApi();

/**
 * @deprecated Use portsApi.list Instead
 */
export const getPorts = (page = 1, pageSize = 50, filters = {}, companyId?: number) => {
    return portsApi.list(companyId || '', { page, page_size: pageSize, ...filters });
};

export const createPort = (data: PortCreate, companyId: string | number) => {
    return portsApi.create(data, companyId);
};

export const updatePort = (id: string | number, data: PortUpdate, companyId: string | number) => {
    return portsApi.update(id, data, companyId);
};

export const deletePort = (id: string | number, companyId: string | number) => {
    return portsApi.delete(id, companyId);
};
