import { ApiValidationError } from "../../Models/Server.Error.model";
import { Body, Controller, Post, Response, Route, SuccessResponse } from "tsoa";

import {
    HomeView,
    PublicOrganizationViewParams,
    PublicOrganizationView,
    PublicOrganizationServiceViewParams,
    PublicOrganizationServiceView,
    UserAppointmentViewParams,
    UserAppointmentView,
} from "../../Models/Views/Views";
import { ViewsService } from "./Views.Service";
import { BotEventParams, BotView } from "../../Models/WapServer/WapServer.Interface";

@Route("views/home")
export class HomeViewController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async homeView(): Promise<HomeView | ApiValidationError> {
        try {
            const view = await new ViewsService().homeView();
            this.setStatus(200);
            return view;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}
@Route("views/appointment")
export class AppointmentViewController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async appointmentView(
        @Body() requestBody: UserAppointmentViewParams
    ): Promise<UserAppointmentView | ApiValidationError> {
        try {
            const view = await new ViewsService().appointmentView(requestBody);
            this.setStatus(200);
            return view;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("views/organization")
export class OrganizationViewController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async homeView(
        @Body() requestBody: PublicOrganizationViewParams
    ): Promise<PublicOrganizationView | ApiValidationError> {
        try {
            const view = await new ViewsService().organizationView(requestBody);
            this.setStatus(200);
            return view;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("views/service")
export class OrganizationServiceViewController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async homeView(
        @Body() requestBody: PublicOrganizationServiceViewParams
    ): Promise<PublicOrganizationServiceView | ApiValidationError> {
        try {
            const view = await new ViewsService().organizationServiceView(requestBody);
            this.setStatus(200);
            return view;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}

@Route("views/wap-server-view")
export class WapServerBotViewController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post()
    public async wapServerView(
        @Body() requestBody: BotEventParams
    ): Promise<BotView | ApiValidationError> {
        try {
            const view = await new ViewsService().botServiceView(requestBody);
            this.setStatus(200);
            return view;
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}
