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

export class ProductsController {
  async create(req: AuthRequest, res: Response, next: NextFunction) {
    try {
      const product = await productsService.create(req.body, req.user!.id);
      
      res.status(201).json(
        ResponseFormatter.success(product, 'تم إنشاء المنتج بنجاح - Product created successfully')
      );
    } catch (error) {
      next(error);
    }
  }

  async findAll(req: AuthRequest, res: Response, next: NextFunction) {
    try {
      const page = parseInt(req.query.page as string) || 1;
      const pageSize = parseInt(req.query.pageSize as string) || 20;
      
      const filters = {
        search: req.query.search,
        categoryId: req.query.categoryId,
        brand: req.query.brand,
      };
      
      const result = await productsService.findAll(filters, page, pageSize);
      
      res.json(ResponseFormatter.paginated(
        result.products,
        result.total,
        result.page,
        result.pageSize
      ));
    } catch (error) {
      next(error);
    }
  }

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

  async update(req: AuthRequest, res: Response, next: NextFunction) {
    try {
      const product = await productsService.update(
        req.params.id,
        req.body,
        req.user!.id
      );
      
      res.json(
        ResponseFormatter.success(product, 'تم تحديث المنتج بنجاح - Product updated successfully')
      );
    } catch (error) {
      next(error);
    }
  }

  async delete(req: AuthRequest, res: Response, next: NextFunction) {
    try {
      await productsService.delete(req.params.id, req.user!.id);
      
      res.json(
        ResponseFormatter.success(null, 'تم حذف المنتج بنجاح - Product deleted successfully')
      );
    } catch (error) {
      next(error);
    }
  }

  async adjustStock(req: AuthRequest, res: Response, next: NextFunction) {
    try {
      const { warehouseId, quantity, type } = req.body;
      
      const result = await productsService.adjustStock(
        req.params.id,
        warehouseId,
        quantity,
        type,
        req.user!.id
      );
      
      res.json(
        ResponseFormatter.success(result, 'تم تعديل المخزون بنجاح - Stock adjusted successfully')
      );
    } catch (error) {
      next(error);
    }
  }

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

export const productsController = new ProductsController();
