import { Request, Response } from "express";
import { UsersService } from "../services/users.service";
import {
    SysAdminPaginateUsersParams,
    SysAdminUpdateProfileParams,
    SysAdminSetVerifiedParams,
    SysAdminDeleteUserParams,
    SysAdminOrganizationsStatusParams,
} from "../models/Users.Model";

const usersService = new UsersService();

export class UsersController {
    public paginateUsers = async (req: Request, res: Response): Promise<void> => {
        try {
            const data: SysAdminPaginateUsersParams = req.body;
            const users = await usersService.paginateUsers(data);
            res.json(users);
        } catch (error: any) {
            console.error("Error paginating users:", error?.response?.data || error);
            res.status(500).json(error?.response?.data || { success: false, message: "Internal Server Error" });
        }
    };

    public updateProfile = async (req: Request, res: Response): Promise<void> => {
        try {
            const data: SysAdminUpdateProfileParams = req.body;
            const result = await usersService.updateProfile(data);
            res.json(result);
        } catch (error: any) {
            console.error("Error updating user profile:", error?.response?.data || error);
            res.status(500).json(error?.response?.data || { success: false, message: "Internal Server Error" });
        }
    };

    public setVerifiedStatus = async (req: Request, res: Response): Promise<void> => {
        try {
            const data: SysAdminSetVerifiedParams = req.body;
            const result = await usersService.setVerifiedStatus(data);
            res.json(result);
        } catch (error: any) {
            console.error("Error setting verified status:", error?.response?.data || error);
            res.status(500).json(error?.response?.data || { success: false, message: "Internal Server Error" });
        }
    };

    public organizationsStatus = async (req: Request, res: Response): Promise<void> => {
        try {
            const data: SysAdminOrganizationsStatusParams = req.body;
            const result = await usersService.organizationsStatus(data);
            res.json(result);
        } catch (error: any) {
            console.error("Error fetching organizations status:", error?.response?.data || error);
            res.status(500).json(error?.response?.data || { success: false, message: "Internal Server Error" });
        }
    };

    public deleteUser = async (req: Request, res: Response): Promise<void> => {
        try {
            const data: SysAdminDeleteUserParams = req.body;
            const result = await usersService.deleteUser(data);
            res.json(result);
        } catch (error: any) {
            console.error("Error deleting user:", error?.response?.data || error);
            res.status(500).json(error?.response?.data || { success: false, message: "Internal Server Error" });
        }
    };
}
