import { Controller, Get, Post, Body, Param, Query, NotFoundException } from '@nestjs/common';
import { ApprovalRepository } from '../../../infrastructure/repositories/approval.repository';
import { ApproveTaskUseCase } from '../../../application/use-cases/approve-task.use-case';

@Controller('api/v1/ops/approvals')
export class ApprovalController {
  constructor(
    private readonly approvalRepository: ApprovalRepository,
    private readonly approveTaskUseCase: ApproveTaskUseCase,
  ) {}

  @Get()
  async listApprovals(@Query('status') status?: string) {
    return this.approveTaskUseCase.getApprovalRequests(status);
  }

  @Get(':id')
  async getApproval(@Param('id') id: string) {
    const approval = await this.approvalRepository.findById(id);
    if (!approval) {
      throw new NotFoundException(`Approval request ${id} not found`);
    }
    return approval;
  }

  @Post(':id/approve')
  async approve(
    @Param('id') id: string,
    @Body() body: { approvedBy: string },
  ) {
    await this.approveTaskUseCase.approve(id, body.approvedBy);
    return { id, status: 'approved', approvedBy: body.approvedBy };
  }

  @Post(':id/reject')
  async reject(
    @Param('id') id: string,
    @Body() body: { rejectedBy: string; reason?: string },
  ) {
    await this.approveTaskUseCase.reject(id, body.rejectedBy, body.reason);
    return { id, status: 'rejected', rejectedBy: body.rejectedBy, reason: body.reason };
  }
}
