import { Request, Response, NextFunction } from 'express';
import * as residenceUnitTypesService from '../services/residenceUnitTypes.service';
import { sendSuccess, sendError } from '../utils/apiResponse';
import { createResidenceUnitTypeSchema, updateResidenceUnitTypeSchema } from '../validators/residenceUnitTypes.validator';
import { processAndSaveMedia } from '../middlewares/upload.middleware';

export const listByProject = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const types = await residenceUnitTypesService.getUnitTypesByProject(parseInt(req.params.id as string));
    return sendSuccess(res, 200, 'Typologies du projet', types);
  } catch (error: any) {
    return sendError(res, 400, error.message);
  }
};

export const getOne = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const unitType = await residenceUnitTypesService.getUnitTypeById(parseInt(req.params.id as string));
    if (!unitType) return sendError(res, 404, 'Typologie introuvable');
    return sendSuccess(res, 200, 'Détail typologie', unitType);
  } catch (error: any) {
    return sendError(res, 400, error.message);
  }
};

export const create = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const validated = createResidenceUnitTypeSchema.parse(req);
    if (req.file) {
      const media = await processAndSaveMedia(req.file, 'plan');
      validated.body.plan_image_url = media.url;
    }
    const result = await residenceUnitTypesService.createUnitType(parseInt(req.params.id as string), validated.body, req.user!.id, req.ip, req.headers['user-agent'] as unknown as string | undefined | undefined);
    return sendSuccess(res, 201, 'Typologie créée', result);
  } catch (error: any) {
    if (error.name === 'ZodError') return sendError(res, 400, 'Données invalides', error.errors);
    return sendError(res, 400, error.message);
  }
};

export const update = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const typeId = parseInt((req.params.id || req.params.typeId) as string);
    const validated = updateResidenceUnitTypeSchema.parse(req);
    if (req.file) {
      const media = await processAndSaveMedia(req.file, 'plan');
      validated.body.plan_image_url = media.url;
    }
    await residenceUnitTypesService.updateUnitType(typeId, validated.body, req.user!.id, req.ip, req.headers['user-agent'] as unknown as string | undefined | undefined);
    return sendSuccess(res, 200, 'Typologie mise à jour');
  } catch (error: any) {
    if (error.name === 'ZodError') return sendError(res, 400, 'Données invalides', error.errors);
    return sendError(res, 400, error.message);
  }
};

export const remove = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const typeId = parseInt((req.params.id || req.params.typeId) as string);
    await residenceUnitTypesService.deleteUnitType(typeId, req.user!.id, req.ip, req.headers['user-agent'] as unknown as string | undefined | undefined);
    return sendSuccess(res, 200, 'Typologie supprimée');
  } catch (error: any) {
    return sendError(res, 400, error.message);
  }
};
