import { Controller, Post, Route, SuccessResponse, Body, Middlewares } from "tsoa";
import { DiscountsService } from "./Discounts.Service";
import {
    CreateDiscountParams,
    DeleteDiscountParams,
    FindDiscountsParams,
    IDiscount,
    PaginateDiscountsParams,
    PaginateDiscountsResults,
} from "../../Models/Discounts/Discounts.Interface";
import { ApiValidationError } from "../../Models/Server.Error.model";
import { ApiVoidResult } from "../../Models/Api.VoidResult.type";
import { authenticateMiddleware } from "../../middleware/authentication";
import { TextObjectFilterResult } from "../../Models/TextObjectFilter.model";

@Route("discounts")
export class DiscountsController extends Controller {
    @SuccessResponse(201, "Created")
    @Post("create")
    @Middlewares(authenticateMiddleware)
    public async createDiscount(
        @Body() requestBody: CreateDiscountParams
    ): Promise<IDiscount | ApiValidationError> {
        try {
            const discount = await new DiscountsService().createDiscount(requestBody);
            this.setStatus(201);
            return discount;
        } 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 deleteDiscount(
        @Body() requestBody: DeleteDiscountParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new DiscountsService().deleteDiscount(requestBody);
            this.setStatus(200);
            return { success: true, message: "Discount 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: PaginateDiscountsParams
    ): Promise<PaginateDiscountsResults | ApiValidationError> {
        try {
            const result = await new DiscountsService().paginate(requestBody);
            this.setStatus(200);
            return result;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }

    @SuccessResponse(200, "Done")
    @Post("filter-all")
    @Middlewares(authenticateMiddleware)
    public async textObjectFilter(
        @Body() requestBody: FindDiscountsParams
    ): Promise<TextObjectFilterResult[] | ApiValidationError> {
        try {
            const result = await new DiscountsService().textObjectFilter(requestBody);
            this.setStatus(200);
            return result;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}
