import {
    Body,
    Controller,
    Middlewares,
    Post,
    Response,
    Route,
    SuccessResponse,
} from "tsoa";

import { ApiValidationError } from "../../../Models/Server.Error.model";
import { ApiVoidResult } from "../../../Models/Api.VoidResult.type";
import { sysAdminAuthMiddleware } from "../../../middleware/sysadminAuth";
import { SysAdminUsersService } from "./SysAdminUsers.Service";
import {
    SysAdminPaginateUsersParams,
    SysAdminUpdateProfileParams,
    SysAdminSetVerifiedParams,
    SysAdminDeleteUserParams,
    SysAdminOrganizationsStatusParams,
    SysAdminUserOrganizationsResult,
    PaginateUsersResults,
} from "../../../Models/Users/Users.Interface";

@Route("sysadmin/users")
@Middlewares(sysAdminAuthMiddleware)
export class SysAdminUsersController extends Controller {
    @Response<ApiValidationError>(500, "Ha ocurrido un error")
    @SuccessResponse(200, "Done")
    @Post("paginate")
    public async paginateUsers(
        @Body() requestBody: SysAdminPaginateUsersParams
    ): Promise<PaginateUsersResults | ApiValidationError> {
        try {
            const users = await new SysAdminUsersService().paginateUsers(requestBody);
            this.setStatus(200);
            return users;
        } 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, "El perfil fue modificado con éxito")
    @Post("update")
    public async updateProfile(
        @Body() requestBody: SysAdminUpdateProfileParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new SysAdminUsersService().updateProfile(requestBody);
            this.setStatus(200);
            return {
                success: true,
                message: "Done",
            };
        } 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, "El estado fue modificado con éxito")
    @Post("set-verified")
    public async setVerifiedStatus(
        @Body() requestBody: SysAdminSetVerifiedParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new SysAdminUsersService().setVerifiedStatus(requestBody);
            this.setStatus(200);
            return {
                success: true,
                message: "Done",
            };
        } 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("organizations-status")
    public async organizationsStatus(
        @Body() requestBody: SysAdminOrganizationsStatusParams
    ): Promise<SysAdminUserOrganizationsResult | ApiValidationError> {
        try {
            const result = await new SysAdminUsersService().organizationsStatus(requestBody);
            this.setStatus(200);
            return result;
        } 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, "El usuario fue eliminado")
    @Post("delete")
    public async deleteUser(
        @Body() requestBody: SysAdminDeleteUserParams
    ): Promise<ApiVoidResult | ApiValidationError> {
        try {
            await new SysAdminUsersService().deleteUser(requestBody);
            this.setStatus(200);
            return {
                success: true,
                message: "Done",
            };
        } catch (e) {
            const errorOccurred: Error = e as Error;
            this.setStatus(500);
            return new ApiValidationError(500, errorOccurred.message);
        }
    }
}
