import { apiRequest } from "@/shared/api/request";
import { API_ENDPOINTS, API_OPERATIONS, apiEndpointPath } from "@/shared/api/generated/endpoints";
import type {
    ApiPage,
    EntityId,
    PageQuery,
    RoomConfigDto,
    RoomConfigPayload,
    RoomWhitelistConfigDto,
    RoomWhitelistPayload,
    RoomDto,
    RoomPinDto,
    RoomPinPayload,
    AvailableRoomRobotDto,
    HumanRoomRobotConfigDto,
    HumanRoomRobotConfigPayload,
    RobotRoomDto,
    RobotRoomPayload,
    RoomUpdatePayload,
} from "@/shared/api/types";

export function listRooms(query: PageQuery = {}): Promise<ApiPage<RoomDto>> {
    const endpoint = API_ENDPOINTS.listRooms;
    return apiRequest<ApiPage<RoomDto>>(apiEndpointPath(API_OPERATIONS.listRooms), {
        method: endpoint.method,
        query,
    });
}

export function updateRoom(roomId: EntityId, payload: RoomUpdatePayload): Promise<RoomDto> {
    const endpoint = API_ENDPOINTS.updateRoom;
    return apiRequest<RoomDto, RoomUpdatePayload>(apiEndpointPath(API_OPERATIONS.updateRoom, { room_id: roomId }), {
        body: payload,
        method: endpoint.method,
    });
}

export function listRoomPins(query: PageQuery = {}): Promise<ApiPage<RoomPinDto>> {
    const endpoint = API_ENDPOINTS.listRoomPins;
    return apiRequest<ApiPage<RoomPinDto>>(apiEndpointPath(API_OPERATIONS.listRoomPins), {
        method: endpoint.method,
        query,
    });
}

export function createRoomPin(payload: RoomPinPayload): Promise<RoomPinDto> {
    const endpoint = API_ENDPOINTS.createRoomPin;
    return apiRequest<RoomPinDto, RoomPinPayload>(apiEndpointPath(API_OPERATIONS.createRoomPin), {
        body: payload,
        method: endpoint.method,
    });
}

export function cancelRoomPin(pinId: EntityId): Promise<RoomPinDto> {
    const endpoint = API_ENDPOINTS.cancelRoomPin;
    return apiRequest<RoomPinDto>(apiEndpointPath(API_OPERATIONS.cancelRoomPin, { pin_id: pinId }), {
        method: endpoint.method,
    });
}

export function deleteRoom(roomId: EntityId): Promise<{ deleted: boolean }> {
    const endpoint = API_ENDPOINTS.deleteRoom;
    return apiRequest<{ deleted: boolean }>(apiEndpointPath(API_OPERATIONS.deleteRoom, { room_id: roomId }), {
        method: endpoint.method,
    });
}

export function getRoomConfig(): Promise<RoomConfigDto> {
    const endpoint = API_ENDPOINTS.getRoomConfig;
    return apiRequest<RoomConfigDto>(apiEndpointPath(API_OPERATIONS.getRoomConfig), {
        method: endpoint.method,
    });
}

export function updateRoomConfig(payload: RoomConfigPayload): Promise<RoomConfigDto> {
    const endpoint = API_ENDPOINTS.updateRoomConfig;
    return apiRequest<RoomConfigDto, RoomConfigPayload>(apiEndpointPath(API_OPERATIONS.updateRoomConfig), {
        body: payload,
        method: endpoint.method,
    });
}

export function getRoomWhitelistConfig(): Promise<RoomWhitelistConfigDto> {
    const endpoint = API_ENDPOINTS.getRoomWhitelistConfig;
    return apiRequest<RoomWhitelistConfigDto>(apiEndpointPath(API_OPERATIONS.getRoomWhitelistConfig), {
        method: endpoint.method,
    });
}

export function updateRoomWhitelistConfig(payload: RoomWhitelistPayload): Promise<RoomWhitelistConfigDto> {
    const endpoint = API_ENDPOINTS.updateRoomWhitelistConfig;
    return apiRequest<RoomWhitelistConfigDto, RoomWhitelistPayload>(
        apiEndpointPath(API_OPERATIONS.updateRoomWhitelistConfig),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function listRobotRooms(query: PageQuery = {}): Promise<ApiPage<RobotRoomDto>> {
    const endpoint = API_ENDPOINTS.listRobotRooms;
    return apiRequest<ApiPage<RobotRoomDto>>(apiEndpointPath(API_OPERATIONS.listRobotRooms), {
        method: endpoint.method,
        query,
    });
}

export function getHumanRoomRobotConfig(): Promise<HumanRoomRobotConfigDto> {
    const endpoint = API_ENDPOINTS.getHumanRoomRobotConfig;
    return apiRequest<HumanRoomRobotConfigDto>(apiEndpointPath(API_OPERATIONS.getHumanRoomRobotConfig), {
        method: endpoint.method,
    });
}

export function updateHumanRoomRobotConfig(payload: HumanRoomRobotConfigPayload): Promise<HumanRoomRobotConfigDto> {
    const endpoint = API_ENDPOINTS.updateHumanRoomRobotConfig;
    return apiRequest<HumanRoomRobotConfigDto, HumanRoomRobotConfigPayload>(
        apiEndpointPath(API_OPERATIONS.updateHumanRoomRobotConfig),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function listAvailableRoomRobots(): Promise<AvailableRoomRobotDto[]> {
    const endpoint = API_ENDPOINTS.listAvailableRoomRobots;
    return apiRequest<AvailableRoomRobotDto[]>(apiEndpointPath(API_OPERATIONS.listAvailableRoomRobots), {
        method: endpoint.method,
    });
}

export function createRobotRoom(payload: RobotRoomPayload): Promise<RobotRoomDto> {
    const endpoint = API_ENDPOINTS.createRobotRoom;
    return apiRequest<RobotRoomDto, RobotRoomPayload>(apiEndpointPath(API_OPERATIONS.createRobotRoom), {
        body: payload,
        method: endpoint.method,
    });
}

export function startRobotRoom(roomId: EntityId): Promise<RobotRoomDto> {
    const endpoint = API_ENDPOINTS.startRobotRoom;
    return apiRequest<RobotRoomDto>(apiEndpointPath(API_OPERATIONS.startRobotRoom, { room_id: roomId }), {
        method: endpoint.method,
    });
}

export function stopRobotRoom(roomId: EntityId): Promise<RobotRoomDto> {
    const endpoint = API_ENDPOINTS.stopRobotRoom;
    return apiRequest<RobotRoomDto>(apiEndpointPath(API_OPERATIONS.stopRobotRoom, { room_id: roomId }), {
        method: endpoint.method,
    });
}
