import { ApiValidationError } from "../../Models/Server.Error.model";
import { Body, Controller, Middlewares, Post, Response, Route, SuccessResponse } from "tsoa";
import { authenticateMiddleware } from "../../middleware/authentication";
import { DashboardService } from "./Dashboard.Service";
import { CompanyMetricsResult, DashboardFinancialsResult, DashboardSummaryResult, GetCompanyMetricsParams, GetDashboardFinancialsParams, GetDashboardSummaryParams } from "./Dashboard.Interface";

@Route("dashboard")
@Middlewares(authenticateMiddleware)
export class DashboardController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post("summary")
    public async getDashboardSummary(
        @Body() requestBody: GetDashboardSummaryParams
    ): Promise<DashboardSummaryResult | ApiValidationError> {
        try {
            const summary = await new DashboardService().getDashboardSummary(requestBody.sessionUser);
            this.setStatus(200);
            return summary;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }

    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post("financials")
    public async getDashboardFinancials(
        @Body() requestBody: GetDashboardFinancialsParams
    ): Promise<DashboardFinancialsResult | ApiValidationError> {
        try {
            const financials = await new DashboardService().getDashboardFinancials(requestBody);
            this.setStatus(200);
            return financials;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }

    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post("company-metrics")
    public async getCompanyMetrics(
        @Body() requestBody: GetCompanyMetricsParams
    ): Promise<CompanyMetricsResult | ApiValidationError> {
        try {
            const metrics = await new DashboardService().getCompanyMetrics(requestBody);
            this.setStatus(200);
            return metrics;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}
