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 { VendorService } from './vendor.service';
import { AuthGuard } from '../auth/auth.guard';
// import { RolesGuard } from 'src/auth/roles.guard';
import { CallService } from '../call/call.service';
@ApiTags('Vendor')
@Controller('vendor')
export class VendorController {
  constructor(private readonly VendorService: VendorService,
    private readonly CallService: CallService
  ) { }

  @Post('signup')
  async signup(@Body() dto: dto.vendor.vendorSignup) {
    try {
      let data = await this.VendorService.signup(dto);
      let response = {
        success: true,
        message:
          "Welcome back! You've successfully logged into your account. Feel free to explore and enjoy our services",
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Post('resend-email-otp')
  async resend_email_otp(@Request() req: any) {
    try {
      let data = await this.VendorService.resend_otp(req);
      let response = {
        success: true,
        message: "success",
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Put('verify-email-otp')
  async verify_email_otp(@Body() dto: dto.vendor.verifyOtp, @Request() req: any) {
    try {
      let data = await this.VendorService.verify_otp(dto, req);
      let response = {
        success: true,
        message: "Success",
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @Post('forget-password')
  async forget_password(@Body() dto: dto.vendor.forgetPassword) {
    try {
      let data = await this.VendorService.forget_password(dto);
      let response = {
        success: true,
        message: "success",
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @Put('verify-forget-password-otp')
  async verify_forget_password(@Body() dto: dto.vendor.forgetPasswordOtpVerify, @Request() req: any) {
    try {
      let data = await this.VendorService.verify_otp_forget_password(dto);
      let response = {
        success: true,
        message: "Success",
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @Put('reset-password')
  async update_password(@Body() dto: dto.vendor.updateForgetPassword, @Request() req: any) {
    try {
      let data = await this.VendorService.reset_password(dto);
      let response = {
        success: true,
        message: "Success",
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @Post('login')
  async login(@Body() dto: dto.vendor.vendorLogin) {
    try {
      let data = await this.VendorService.login(dto);
      let response = {
        success: true,
        message:
          "Welcome back! You've successfully logged into your account. Feel free to explore and enjoy our services",
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }


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

  @Get('profile-chatboat')
  // @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  async get_profile_for_chatboat(@Request() req: any) {
    try {
      let data = await this.VendorService.super_admin_profile();
      let response = {
        success: true,
        message: "Success",
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @Put('profile')
  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  async edit_admin_profile(@Body() dto: dto.editProfile, @Request() req: any) {
    try {
      let data = await this.VendorService.edit_profile(dto, req);
      let response = {
        message: 'Logged in',
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Put('password')
  async admin_change_password(
    @Body() dto: dto.adminChangePass,
    @Request() req: any
  ) {
    try {
      let data = await this.VendorService.changePassword(dto, req);
      let response = {
        message:
          'Your password has been changed successfully. You can now log in with your new password.',
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Get('call')
  async call_list(
    @Query() dto: dto.call.callList,
    @Request() req: any
  ) {
    try {
      let data = await this.VendorService.call_list(dto,req);
      let response = {
        message: "success",
        success: true,
        count: data.count,
        data: data.data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Get('call/:_id')
  async call_detail(
    @Param("_id") call_id: string,
    @Request() req: any
  ) {
    try {
      console.log("call_id", call_id);

      let data = await this.VendorService.call_detail(call_id);
      let response = {
        message: "success",
        success: true,
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }


  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Get('call/:_id/transcript')
  async call_transcript(
    @Param("_id") call_id: string,
    @Request() req: any
  ) {
    try {
      let data = await this.VendorService.call_transcript(call_id);
      let response = {
        message: "success",
        success: true,
        data: data.data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Get('voice-chat/:_id/transcript')
  async voice_call_transcript(
    @Param("_id") chat_id: string,
    @Request() req: any
  ) {
    try {
      let data = await this.VendorService.voice_chat_transcript(chat_id);
      let response = {
        message: "success",
        success: true,
        data: data.data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

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

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Get('dashboard/chat-count')
  async dashboardChatCount(
    @Query() body: dto.DashboardApi,
    @Request() req: any
  ) {
    try {
      let data = await this.VendorService.dashboard_chat_count(body, req);
      let response = {
        message: "success",
        success: true,
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Get('workspace/users')
  async get_users(
    @Query() body: dto.pl,
    @Request() req: any
  ) {
    try {
      let data = await this.VendorService.get_users(body, req);
      let response = {
        message: "success",
        success: true,
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Post('create-workspace')
  async create_wokspace(@Body() dto: dto.vendor.workspaceCreate, @Request() req: any) {
    try {
      let data = await this.VendorService.workspace_create(dto, req);
      let response = {
        success: true,
        message: "Success",
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Put('workspace/:_id/delete')
  async delete_wokspace(@Param("_id") workspace_id: string, @Body() body: dto.vendor.vendor_password, @Request() req: any) {
    try {
      let data = await this.VendorService.delete_workspace(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')
  async wokspace_edit(@Body() dto: dto.vendor.workspaceEdit, @Param("_id") workspace_id: string, @Request() req: any) {
    try {
      let data = await this.VendorService.workspace_edit(workspace_id, dto, req);
      let response = {
        success: true,
        message: "Success",
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Get('workspace')
  async get_wokspace(@Query() dto: dto.vendor.get_workspace, @Request() req: any) {
    try {
      let data = await this.VendorService.get_vendor_workspaces(dto, req);
      let response = {
        success: true,
        message: "Success",
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

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

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

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Post('workspace/:_id/invite')
  async users(@Param("_id") workspace_id: string, @Body() body: dto.vendor.vendor_email, @Request() req: any) {
    try {
      let data = await this.VendorService.invite_user_in_workspace(workspace_id, body, req);
      let response = {
        message: "success",
        success: true,
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  // @UseGuards(AuthGuard)
  // @ApiBearerAuth('authorization')
  @Post('workspace/:_id/resend-invite')
  async resend_invite(@Param("_id") workspace_id: string, @Body() body: dto.vendor.resend_invite, @Request() req: any) {
    try {
      let data = await this.VendorService.resend_invite_user_in_workspace(workspace_id, body, req);
      let response = {
        message: "success",
        success: true,
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Put('workspace/:_id/role')
  async workspace_role_change(@Param("_id") workspace_id: string, @Body() body: dto.vendor.workspace_role_change, @Request() req: any) {
    try {
      let data = await this.VendorService.workspace_role_change(workspace_id, body, req);
      let response = {
        message: "success",
        success: true,
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

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

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Post('contact_us')
  async contact_us(@Body() body: dto.vendor.contact_us, @Request() req: any) {
    try {
      let data = await this.VendorService.contact_us(body, req);
      let response = {
        message: "success",
        success: true,
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Post('initial-message')
  async initial_message(
    @Body() body: dto.message,
    @Request() req: any
  ) {
    try {
      let data = await this.CallService.update_initial_message(body?.message)
      let response = {
        message: "success",
        success: true,
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Get('initial-message')
  async get_message(
    @Request() req: any
  ) {
    try {
      let data = await this.CallService.get_initial_message()
      let response = {
        message: "success",
        success: true,
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Post('workspace/:_id/script')
  async workspace_script_create(
    @Param('_id') workspace_id: string,
    @Body() body: dto.vendor.workspace_script_create,
    @Request() req: any
  ) {
    try {
      let data = await this.VendorService.workspace_script_create(workspace_id, body, req)
      let response = {
        message: "success",
        success: true,
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Put('workspace/:_id/script')
  async workspace_script_edit(
    @Param('_id') workspace_id: string,
    @Body() body: dto.vendor.workspace_script_edit,
    @Request() req: any
  ) {
    try {
      let data = await this.VendorService.workspace_script_edit(workspace_id, body, req)
      let response = {
        message: "success",
        success: true,
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

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

  // @UseGuards(AuthGuard)
  // @ApiBearerAuth('authorization')
  @Get('workspace-script/:_id')
  async workspace_script_detail(
    @Param('_id') script_id: string,
    @Request() req: any
  ) {
    try {
      let data = await this.VendorService.workspace_script_detail_no_auth(script_id, req)
      let response = {
        message: "success",
        success: true,
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Get('workspace/:_id/script')
  async workspace_script_get(
    @Param('_id') workspace_id: string,
    @Body() body: dto.vendor.get_script,
    @Request() req: any
  ) {
    try {
      let data = await this.VendorService.workspace_script_get(workspace_id, body, req)
      let response = {
        message: "success",
        success: true,
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

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

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Patch('workspace/:_id/script/:script_id')
  async script_enable_disable(
    @Param('_id') workspace_id: string,
    @Param('script_id') script_id: string,
    @Body() body: dto.vendor.workspace_script_status,
    @Request() req: any
  ) {
    try {
      let data = await this.VendorService.script_enable_disable(workspace_id, script_id, req, body)
      let response = {
        message: "success",
        success: true,
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

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

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

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

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Post('white-list-form')
  async white_list_form(
    @Body() body: dto.vendor.white_list_form,
    @Request() req: any
  ) {
    try {
      let data = await this.VendorService.white_list_form(body, req)
      let response = {
        message: "success",
        success: true,
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Post('white-label')
  async white_label_form(
    @Body() body: dto.vendor.white_list_form,
    @Request() req: any
  ) {
    try {
      let data = await this.VendorService.white_list_form(body, req)
      let response = {
        message: "success",
        success: true,
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth('authorization')
  @Get('exist-count')
  async get_exist_count(
    @Request() req: any
  ) {
    try {
      let data = await this.VendorService.get_exist_count(req)
      let response = {
        message: "success",
        success: true,
        data: data,
      };
      return response;
    } catch (err) {
      throw err;
    }
  }


  @Get('whitelabel/callback')
  async whitelabel_callback(@Request() req: any) {
    try {
      console.log("INSIDE CALLBACK")
      console.log('whitelabel/callback ==> req.query', req.query)
      console.log('whitelabel/callback ==> req.body', req.body)
    } catch (err) {
      throw err;
    }
  }

}
