import { Response, NextFunction } from 'express';
import { AuthRequest } from '../../shared/middleware/auth';
import { costSheetsService } from './costSheets.service';
import { ResponseFormatter } from '../../shared/utils/responseFormatter';

export class CostSheetsController {
  async create(req: AuthRequest, res: Response, next: NextFunction) {
    try {
      const cs = await costSheetsService.create(req.body, req.user!.id);
      res.status(201).json(
        ResponseFormatter.success(cs, 'تم إنشاء كشف التكلفة - Cost sheet created')
      );
    } catch (error) {
      next(error);
    }
  }

  async findById(req: AuthRequest, res: Response, next: NextFunction) {
    try {
      const cs = await costSheetsService.findById(req.params.id);
      res.json(ResponseFormatter.success(cs));
    } catch (error) {
      next(error);
    }
  }

  async findByDeal(req: AuthRequest, res: Response, next: NextFunction) {
    try {
      const list = await costSheetsService.findByDeal(req.params.dealId);
      res.json(ResponseFormatter.success(list));
    } catch (error) {
      next(error);
    }
  }

  async approve(req: AuthRequest, res: Response, next: NextFunction) {
    try {
      const cs = await costSheetsService.approve(req.params.id, req.user!.id, req.user!.id);
      res.json(ResponseFormatter.success(cs, 'تمت الموافقة على كشف التكلفة - Cost sheet approved'));
    } catch (error) {
      next(error);
    }
  }

  async reject(req: AuthRequest, res: Response, next: NextFunction) {
    try {
      const cs = await costSheetsService.reject(req.params.id, req.user!.id);
      res.json(ResponseFormatter.success(cs, 'تم رفض كشف التكلفة - Cost sheet rejected'));
    } catch (error) {
      next(error);
    }
  }
}

export const costSheetsController = new CostSheetsController();
