import {
    Body,
    Controller,
    Delete,
    Get,
    Param,
    Post,
    Put,
    Query,
    UseGuards,
    Request,
    Patch,
} from '@nestjs/common';
// import { AuthGuard } from 'src/auth/auth.guard';

import { ApiBearerAuth, ApiTags, ApiOperation } from '@nestjs/swagger';
import * as dto from './dto/index';
import { AdminService } from './admin.service';
import { AuthGuard } from '../auth/auth.guard';
// import { RolesGuard } from 'src/auth/roles.guard';
import { CallService } from '../call/call.service';
import { NotificationService } from './notification/notification.service';

@ApiTags('admin')
@Controller('admin')
export class AgentController {
    constructor(
        private readonly AdminService: AdminService,
        private readonly NotificationService: NotificationService,
        private readonly CallService: CallService
    ) { }


    @Post('login')
    async login_admin(@Body() dto: dto.admin.Login, @Request() req: any) {
        try {
            let data = await this.AdminService.login(dto);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('profile')
    async admin_profile(@Request() req: any) {
        try {
            let data = await this.AdminService.profile_get(req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @ApiOperation({ summary: "Edit Profile API" })
    @Put("/profile")
    async editProfile(@Body() dto: dto.admin.editProfileAdmin, @Request() req: any) {
        try {
            let response = await this.AdminService.editProfile(dto, req);
            return {
                success: true,
                message: `Edit Successfully`,
                data: response
            };
        } catch (err) {
            throw err;
        }
    }


    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @ApiOperation({ summary: "Edit Profile API" })
    @Put("/password")
    async changePassword(@Body() dto: dto.admin.adminChangePass, @Request() req: any,) {
        try {
            let response = await this.AdminService.changePassword(dto, req);
            return {
                success: true,
                message: `Edit Successfully`,
                data: response
            };
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('workspace')
    async admin_workspace_get(@Query() body: dto.admin.get_workspace, @Request() req: any) {
        try {
            let data = await this.AdminService.get_workpace(body, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('workspace/:_id')
    async admin_workspace_detail(@Param('_id') workspace_id: string, @Request() req: any) {
        try {
            let data = await this.AdminService.get_workpace_detail(workspace_id, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('workspace/:_id/users')
    async admin_workspace_users(@Param('_id') workspace_id: string, @Query() body: dto.admin.get_workspace, @Request() req: any) {
        try {
            let data = await this.AdminService.get_workpace_members(workspace_id, body, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('workspace/:_id/subscription')
    async get_workpace_subscription(@Param('_id') workspace_id: string, @Query() body: dto.admin.get_workspace, @Request() req: any) {
        try {
            let data = await this.AdminService.get_workpace_subscription(workspace_id, body, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Put('workspace/:_id/block')
    async admin_workspace_block(@Param('_id') workspace_id: string, @Query() body: dto.admin.workspace_status, @Request() req: any) {
        try {
            let data = await this.AdminService.workpace_status_change(workspace_id, body, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('vendor')
    async admin_vendor_get(@Query() body: dto.admin.get_vendor, @Request() req: any) {
        try {
            let data = await this.AdminService.get_vendor(body, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('vendor/:_id')
    async admin_vendor_detail(@Param('_id') vendor_id: string, @Request() req: any) {
        try {
            let data = await this.AdminService.get_vendor_detail(vendor_id, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('vendor/:_id/workspace')
    async admin_vendor_workspace(@Param('_id') vendor_id: string, @Request() req: any) {
        try {
            let data = await this.AdminService.get_vendor_workspace(vendor_id, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Patch('vendor/:_id/block')
    async admin_vendor_block(@Param('_id') vendor_id: string, @Query() body: dto.admin.vendor_status, @Request() req: any) {
        try {
            let data = await this.AdminService.vendor_status_change(vendor_id, body, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('subscription/plan')
    async subscription_plan_list_get(@Query() body: dto.admin.get_subscription, @Request() req: any) {
        try {
            let data = await this.AdminService.subscription_plans(body);
            let response = {
                success: true,
                message: `Subscription plan found successfully.`,
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    // @UseGuards(AuthGuard)
    // @ApiBearerAuth('authorization')
    @Get('accounting')
    async accounting_list_get(@Query() body: dto.admin.get_accounting, @Request() req: any) {
        try {
            let data = await this.AdminService.accounting_subscription(body);
            let response = {
                success: true,
                message: `Subscription found successfully.`,
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('/white-label')
    async white_label_lists(@Query() body: dto.admin.get_white_label_list, @Request() req: any) {
        try {
            let data = await this.AdminService.white_label_list(body);
            let response = {
                success: true,
                message: `White label request found successfully.`,
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @Get('notification/vendor')
    async vendor_list(@Query() body: dto.admin.get_vendor, @Request() req: any) {
        try {
            let data = await this.NotificationService.get_vendor_list(body);
            let response = {
                success: true,
                message: "vendor found succesfully",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }  

    @Post('notification/send')
    async bulk_notification(@Body() body: dto.admin.bulk_notification, @Request() req: any) {
        try {
            let data = await this.NotificationService.bulk_notification(body);
            let response = {
                success: true,
                message: "Notification send succesfully",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }    
    
    @Get('dashboard/overview')
    async dashboard_counts(@Query() dto: dto.admin.DashboardApi) {
        const data = await this.AdminService.dashboard_counts(dto);
        const response = {
            success: true,
            message: "Dashboard count found succesfully",
            data: data,
        };
        return response
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('dashboard/subscription')
    async subscription_list_get(@Query() body: dto.admin.get_subscription, @Request() req: any) {
        try {
            let data = await this.AdminService.recent_subscription(body);
            let response = {
                success: true,
                message: `Subscription found successfully.`,
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('dashboard/vendors')
    async recent_vendors_list(@Query() body: dto.admin.get_recent_vendor, @Request() req: any) {
        try {
            let data = await this.AdminService.recent_vendors(body);
            let response = {
                success: true,
                message: `Recent vendors found successfully.`,
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Post('workspace/create')
    async create_workspace(@Body() dto: dto.admin.workspace_create, @Request() req: any) {
        try {
            let data = await this.AdminService.create_workspace(req, dto);
            let response = {
                success: true,
                message: `Workspace created succesfully!`,
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('my-workspace')
    async admin_workspace_list(@Request() req: any) {
        try {
            let data = await this.AdminService.get_admin_workspace(req);
            let response = {
                success: true,
                message: `Workspace found succesfully!`,
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @Get('secret/gen-key')
    async generate_workspace_keys() {
        try {
            let data = await this.AdminService.get_secret_key(null);
            let response = {
                success: true,
                message: `Secret key`,
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('contactus')
    async admin_contact_us(@Query() body: dto.admin.pl, @Request() req: any) {
        try {
            let data = await this.AdminService.contact_us_list(body, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('contactus/:_id')
    async admin_contact_us_detail(@Param('_id') contact_us_id: string, @Request() req: any) {
        try {
            let data = await this.AdminService.contact_us_detail(contact_us_id, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Put('contactus/:_id')
    async admin_contact_us_edit(@Param('_id') contact_us_id: string, @Body() body: dto.admin.contact_us_edit, @Request() req: any) {
        try {
            let data = await this.AdminService.contact_us_edit(contact_us_id, body, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Delete('contactus/:_id')
    async admin_contact_us_delete(@Param('_id') contact_us_id: string, @Request() req: any) {
        try {
            let data = await this.AdminService.contact_us_delete(contact_us_id, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }


    @UseGuards(AuthGuard)
    @ApiBearerAuth("authorization")
    @Post('/content')
    async add_content(@Body() dto: dto.admin.AddContent) {
        try {
            let data = await this.AdminService.add_content(dto);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (error) {
            throw error
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth("authorization")
    @Put('/content')
    async edit_content(@Body() dto: dto.admin.EditContent) {
        try {
            let data = await this.AdminService.edit_content(dto);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (error) {
            throw error
        }
    }


    @UseGuards(AuthGuard)
    @ApiBearerAuth("authorization")
    @Delete('content/:_id')
    async delete_content(@Param() dto: dto.admin.ContentDetails) {
        return this.AdminService.delete_content(dto);
    }

    // @Get('content')
    // async list_content(@Query() dto: dto.admin.ListContent) {
    //     return this.AdminService.list_content(dto);
    // }
    @Get('content')
    async list_content() {
        return this.AdminService.list_content();
    }


    @Get('content/:_id')
    async content_details(@Param() dto: dto.admin.ContentDetails) {
        return this.AdminService.content_details(dto);
    }
}
