import {
  Controller, Get, Post, Patch, Delete,
  Param, Body, Query, HttpCode, HttpStatus,
} from '@nestjs/common'
import { MatterDeadlinesService } from './matter-deadlines.service'

@Controller('matter-deadlines')
export class MatterDeadlinesController {
  constructor(private readonly svc: MatterDeadlinesService) {}

  // ── Obligation rules ────────────────────────────────────────────────────────

  @Get('rules')
  findAllRules(@Query('practiceArea') practiceArea?: string) {
    return this.svc.findAllRules(practiceArea)
  }

  @Post('rules/seed')
  seedRules() {
    return this.svc.seedRules()
  }

  // ── Deadline queries ────────────────────────────────────────────────────────

  @Get('matter/:matterId')
  findByMatter(@Param('matterId') matterId: string) {
    return this.svc.findByMatter(matterId)
  }

  @Get('client/:clientFolderId')
  findByClient(@Param('clientFolderId') clientFolderId: string) {
    return this.svc.findByClient(clientFolderId)
  }

  @Get('upcoming')
  findUpcoming(@Query('daysAhead') daysAhead?: string) {
    return this.svc.findUpcoming(daysAhead ? Number(daysAhead) : 30)
  }

  // ── Generate proposals ──────────────────────────────────────────────────────

  @Post('matter/:matterId/generate')
  generateProposals(@Param('matterId') matterId: string) {
    return this.svc.generateProposals(matterId)
  }

  // ── Manual create ───────────────────────────────────────────────────────────

  @Post()
  manualCreate(@Body() body: {
    matterId: string
    name: string
    description?: string
    dueDate: string
    reminderLeadDays?: number
    linkedFormIds?: string[]
    notes?: string
  }) {
    return this.svc.manualCreate(body)
  }

  // ── Confirmation actions ────────────────────────────────────────────────────

  @Patch(':id/confirm')
  confirm(
    @Param('id') id: string,
    @Body() body: { confirmedBy?: string; dueDate?: string; reminderDate?: string; notes?: string; linkedFormIds?: string[] },
  ) {
    const { confirmedBy = 'attorney', ...edits } = body
    return this.svc.confirm(id, confirmedBy, edits)
  }

  @Patch(':id/reject')
  reject(
    @Param('id') id: string,
    @Body() body: { reason?: string },
  ) {
    return this.svc.reject(id, body.reason)
  }

  @Patch(':id/complete')
  complete(@Param('id') id: string) {
    return this.svc.complete(id)
  }

  // ── Delete ──────────────────────────────────────────────────────────────────

  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  remove(@Param('id') id: string) {
    return this.svc.remove(id)
  }
}
