import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  ParseUUIDPipe,
} from '@nestjs/common';
import { AgentCostService } from './agent-cost.service';
import { AssignAgentsDto, CreateAgentCostDto, UpdateAgentCostDto } from './dto';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { PermissionGuard } from '../../auth/guards/permission.guard';
import { Permissions } from '../../auth/decorators/permissions.decorator';
import { AccessLevel, ModuleName } from '../../entities/role-permission.entity';
import { paginatedResponse, successResponse } from '../../common/responses/api-response';
import { PaginationDto } from '../../common/dto/pagination.dto';
import { Lookup } from '../../common/decorators/lookup.decorator';

@Controller('agent-costs')
@UseGuards(JwtAuthGuard, PermissionGuard)
export class AgentCostController {
  constructor(private readonly service: AgentCostService) {}

  @Get()
  @Permissions({ module: ModuleName.SETTINGS, accessLevel: AccessLevel.VIEW })
  async findAll(@Query() query: PaginationDto) {
    const { items, meta } = await this.service.findAll(query);
    return paginatedResponse(items, meta, 'Agent costs fetched successfully');
  }

  @Get('lookup')
  @Lookup()
  async lookup() {
    const items = await this.service.findAllForLookup();
    return successResponse(items, 'Agent costs lookup');
  }

  @Get('unassigned-agents')
  @Permissions({ module: ModuleName.SETTINGS, accessLevel: AccessLevel.VIEW })
  async unassignedAgents() {
    const items = await this.service.listUnassignedAgents();
    return successResponse(items, 'Unassigned agents fetched successfully');
  }

  @Get(':id')
  @Permissions({ module: ModuleName.SETTINGS, accessLevel: AccessLevel.VIEW })
  async findById(@Param('id', ParseUUIDPipe) id: string) {
    const data = await this.service.findById(id);
    return successResponse(data, 'Agent cost fetched successfully');
  }

  @Post()
  @Permissions({ module: ModuleName.SETTINGS, accessLevel: AccessLevel.CREATE })
  async create(@Body() dto: CreateAgentCostDto) {
    const result = await this.service.create(dto);
    return successResponse(result, result.message);
  }

  @Patch(':id')
  @Permissions({ module: ModuleName.SETTINGS, accessLevel: AccessLevel.EDIT })
  async update(@Param('id', ParseUUIDPipe) id: string, @Body() dto: UpdateAgentCostDto) {
    const result = await this.service.update(id, dto);
    return successResponse(result, result.message);
  }

  @Post(':id/agents')
  @Permissions({ module: ModuleName.SETTINGS, accessLevel: AccessLevel.EDIT })
  async assign(@Param('id', ParseUUIDPipe) id: string, @Body() dto: AssignAgentsDto) {
    const result = await this.service.assign(id, dto);
    return successResponse(result, result.message);
  }

  @Delete(':id/agents/:tripSourceId')
  @Permissions({ module: ModuleName.SETTINGS, accessLevel: AccessLevel.EDIT })
  async unassign(
    @Param('id', ParseUUIDPipe) id: string,
    @Param('tripSourceId', ParseUUIDPipe) tripSourceId: string,
  ) {
    const result = await this.service.unassign(id, tripSourceId);
    return successResponse(result, result.message);
  }

  @Delete(':id')
  @Permissions({ module: ModuleName.SETTINGS, accessLevel: AccessLevel.DELETE })
  async remove(@Param('id', ParseUUIDPipe) id: string) {
    const result = await this.service.remove(id);
    return successResponse(result, result.message);
  }
}
