import { Controller, Post, Route, SuccessResponse, Body, Middlewares } from "tsoa";
import { FavouritesService } from "./Favourites.Service";
import {
    DeleteFavouriteParams,
    IFavourite,
    CreateFavouriteParams,
    PaginateFavouritesParams,
    PaginateFavouritesResults,
} from "../../Models/Favourites/Favourites.interface";
import { ApiVoidResult } from "../../Models/Api.VoidResult.type";
import { ApiValidationError } from "../../Models/Server.Error.model";
import { authenticateMiddleware } from "../../middleware/authentication";

@Route("favourites")
export class FavouritesController extends Controller {
    @SuccessResponse(201, "Created")
    @Post("create")
    @Middlewares(authenticateMiddleware)
    public async createFavourite(
        @Body() requestBody: CreateFavouriteParams
    ): Promise<IFavourite | ApiValidationError> {
        try {
            const favourite = await new FavouritesService().createFavourite(requestBody);
            this.setStatus(201);
            return favourite;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }

    @SuccessResponse(200, "Done")
    @Post("delete")
    @Middlewares(authenticateMiddleware)
    public async deleteFavourite(
        @Body() requestBody: DeleteFavouriteParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new FavouritesService().deleteFavourite(requestBody);
            this.setStatus(200);
            return { success: true, message: "Favourite deleted successfully." };
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }

    @SuccessResponse(200, "Done")
    @Post("paginate")
    @Middlewares(authenticateMiddleware)
    public async paginate(
        @Body() requestBody: PaginateFavouritesParams
    ): Promise<PaginateFavouritesResults | ApiValidationError> {
        try {
            const result = await new FavouritesService().paginate(requestBody);
            this.setStatus(200);
            return result;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}
