import { Body, Controller, Delete, Get, Param, Patch, Post, Query, UseGuards } from '@nestjs/common';
import { JwtAuthGuard } from '../../common/auth/jwt-auth.guard';
import { RequireTenantGuard } from '../../common/auth/require-tenant.guard';
import { PermissionsGuard } from '../../common/auth/permissions.guard';
import { RequirePermission } from '../../common/auth/permissions.decorator';
import { CurrentTenant } from '../../common/auth/current-tenant.decorator';
import { CurrentUser } from '../../common/auth/current-user.decorator';
import { RequestTenantContext } from '../../common/tenancy/tenant-query.helper';
import type { CrmActor } from '../../common/crm/crm-record.helper';
import { CreateNoteDto } from '../../common/dto/create-note.dto';
import { CustomersService } from './customers.service';
import { CreateCustomerDto } from './dto/create-customer.dto';
import { UpdateCustomerDto } from './dto/update-customer.dto';
import { ListCustomersQuery } from './dto/list-customers.query';

@Controller(['customers', 'accounts'])
@UseGuards(JwtAuthGuard, RequireTenantGuard, PermissionsGuard)
@RequirePermission('accounts', 'read')
export class CustomersController {
  constructor(private readonly customers: CustomersService) {}

  // Returns paginated list of customers with built-in filters. GET /api/customers
  @Get()
  list(@CurrentTenant() t: RequestTenantContext, @Query() q: ListCustomersQuery) {
    return this.customers.list(t, q);
  }

  // Returns customer tab counts and summary metrics. GET /api/customers/summary
  @Get('summary')
  summary(@CurrentTenant() t: RequestTenantContext) {
    return this.customers.counts(t);
  }

  // Returns lightweight customer options for dropdown pickers. GET /api/customers/options
  @Get('options')
  options(@CurrentTenant() t: RequestTenantContext) {
    return this.customers.options(t);
  }

  // Checks potential duplicate customer by name or website. GET /api/customers/check-duplicate
  @Get('check-duplicate')
  checkDuplicate(@CurrentTenant() t: RequestTenantContext, @Query('name') name?: string, @Query('website') website?: string, @Query('excludeId') excludeId?: string) {
    return this.customers.checkDuplicate(t, name, website, excludeId);
  }

  // Returns what deleting a customer would remove and whether it is blocked. GET /api/customers/:id/delete-check
  @Get(':id/delete-check')
  deleteCheck(@CurrentTenant() t: RequestTenantContext, @Param('id') id: string) {
    return this.customers.deleteCheck(t, id);
  }

  // Returns a single customer by ID. GET /api/customers/:id
  @Get(':id')
  get(@CurrentTenant() t: RequestTenantContext, @Param('id') id: string) {
    return this.customers.get(t, id);
  }

  // Creates a new customer. POST /api/customers
  @Post()
  @RequirePermission('accounts', 'create')
  create(@CurrentTenant() t: RequestTenantContext, @Body() dto: CreateCustomerDto) {
    return this.customers.create(t, dto);
  }

  // Updates a customer by ID. PATCH /api/customers/:id
  @Patch(':id')
  @RequirePermission('accounts', 'update')
  update(@CurrentTenant() t: RequestTenantContext, @Param('id') id: string, @Body() dto: UpdateCustomerDto) {
    return this.customers.update(t, id, dto);
  }

  // Deletes a customer by ID. DELETE /api/customers/:id
  @Delete(':id')
  @RequirePermission('accounts', 'delete')
  remove(@CurrentTenant() t: RequestTenantContext, @CurrentUser() user: CrmActor, @Param('id') id: string) {
    return this.customers.remove(t, user, id);
  }

  // Returns activity notes for a customer. GET /api/customers/:id/notes
  @Get(':id/notes')
  notes(@CurrentTenant() t: RequestTenantContext, @Param('id') id: string) {
    return this.customers.listNotes(t, id);
  }

  // Adds an activity note to a customer. POST /api/customers/:id/notes
  @Post(':id/notes')
  @RequirePermission('accounts', 'update')
  addNote(@CurrentTenant() t: RequestTenantContext, @Param('id') id: string, @Body() dto: CreateNoteDto) {
    return this.customers.addNote(t, id, dto);
  }
}
