import { FindProvinciasParams } from "../../Models/Provincias/Provincias.interface";
import { ApiValidationError } from "../../Models/Server.Error.model";
import { Body, Controller, Post, Response, Route, SuccessResponse } from "tsoa";
import { TextObjectFilterResult } from "../../Models/TextObjectFilter.model";
import { LocationService } from "./Location.Service";
import { FindPartidosParams } from "../../Models/Partidos/Partidos.interface";
import {
    FindLocalidadesParams,
    PaginateFilterBlockParams,
} from "../../Models/Localidades/Localidades.interface";

@Route("location/provincias-filter-all")
export class AllProvinciasController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async allProvincias(
        @Body() requestBody: FindProvinciasParams
    ): Promise<TextObjectFilterResult[] | ApiValidationError> {
        try {
            const provincias = await new LocationService().textObjectFilterProvincias(requestBody);
            this.setStatus(200);
            return provincias;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("location/partidos-filter-all")
export class AllPartidosController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async allPartidos(
        @Body() requestBody: FindPartidosParams
    ): Promise<TextObjectFilterResult[] | ApiValidationError> {
        try {
            const partidos = await new LocationService().textObjectFilterPartidos(requestBody);
            this.setStatus(200);
            return partidos;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("location/localidades-filter-all")
export class AllLocalidadesController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async allLocalidades(
        @Body() requestBody: FindLocalidadesParams
    ): Promise<TextObjectFilterResult[] | ApiValidationError> {
        try {
            const localidades = await new LocationService().textObjectFilterLocalidades(
                requestBody
            );
            this.setStatus(200);
            return localidades;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("location/localidades-filter-paginate")
export class LocalidadesFilterPaginateController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async paginateFilterLocalidades(
        @Body() requestBody: PaginateFilterBlockParams
    ): Promise<TextObjectFilterResult[] | ApiValidationError> {
        try {
            const localidades = await new LocationService().paginateFilterLocalidades(requestBody);
            this.setStatus(200);
            return localidades;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}
