import { Controller, Post, Route, SuccessResponse, Body, Middlewares } from "tsoa";
import { ApiValidationError } from "../../Models/Server.Error.model";
import { ApiVoidResult } from "../../Models/Api.VoidResult.type";
import { authenticateMiddleware } from "../../middleware/authentication";
import {
    CreateDiscountEmailParams,
    DeleteDiscountEmailParams,
    IDiscountEmail,
    PaginateDiscountsEmailParams,
    PaginateDiscountsEmailViewResults,
} from "../../Models/DiscountsEmail/DiscountsEmail.Interface";
import { DiscountsEmailService } from "./DiscountsEmail.Service";

@Route("discounts-email")
export class DiscountsEmailController extends Controller {
    @SuccessResponse(201, "Created")
    @Post("create")
    @Middlewares(authenticateMiddleware)
    public async createDiscountEmail(
        @Body() requestBody: CreateDiscountEmailParams
    ): Promise<IDiscountEmail | ApiValidationError> {
        try {
            const discount = await new DiscountsEmailService().createDiscountEmail(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 deleteDiscountEmail(
        @Body() requestBody: DeleteDiscountEmailParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new DiscountsEmailService().deleteDiscountEmail(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: PaginateDiscountsEmailParams
    ): Promise<PaginateDiscountsEmailViewResults | ApiValidationError> {
        try {
            const result = await new DiscountsEmailService().paginate(requestBody);
            this.setStatus(200);
            return result;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}
