import { Request, Response, NextFunction } from 'express';
import { sendSuccess, sendError } from '../utils/apiResponse';
import * as residenceProjectsRepo from '../repositories/residenceProjects.repository';
import * as residenceUnitTypesRepo from '../repositories/residenceUnitTypes.repository';
import * as residenceUnitsRepo from '../repositories/residenceUnits.repository';
import * as constructionRepo from '../repositories/construction.repository';
import * as propertyMediaRepo from '../repositories/propertyMedia.repository'; // or a separate repo if project media exists

export const listProjects = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const projects = await residenceProjectsRepo.findAll();
    // Return only active/planned projects, excluding DRAFT or CANCELLED if we want
    const publicProjects = projects.filter(p => p.status === 'PLANNED' || p.status === 'UNDER_CONSTRUCTION' || p.status === 'COMPLETED');
    return sendSuccess(res, 200, 'Liste des projets', publicProjects);
  } catch (error: any) {
    return sendError(res, 400, error.message);
  }
};

export const getProjectBySlug = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const project = await residenceProjectsRepo.findBySlug(req.params.slug as string);
    if (!project) return sendError(res, 404, 'Projet introuvable');
    return sendSuccess(res, 200, 'Détail du projet', project);
  } catch (error: any) {
    return sendError(res, 400, error.message);
  }
};

export const getProjectMedia = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const project = await residenceProjectsRepo.findBySlug(req.params.slug as string);
    if (!project) return sendError(res, 404, 'Projet introuvable');
    // For now we don't have a residence_project_media repository, let's just return an empty array if not implemented
    const media = await residenceProjectsRepo.getProjectMedia(project.id);
    return sendSuccess(res, 200, 'Médias du projet', media);
  } catch (error: any) {
    return sendError(res, 400, error.message);
  }
};

export const getProjectTypes = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const project = await residenceProjectsRepo.findBySlug(req.params.slug as string);
    if (!project) return sendError(res, 404, 'Projet introuvable');
    const types = await residenceUnitTypesRepo.findAllByProject(project.id);
    return sendSuccess(res, 200, 'Typologies', types);
  } catch (error: any) {
    return sendError(res, 400, error.message);
  }
};

export const getProjectUnits = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const project = await residenceProjectsRepo.findBySlug(req.params.slug as string);
    if (!project) return sendError(res, 404, 'Projet introuvable');
    const units = await residenceUnitsRepo.findAllByProject(project.id);
    
    // STRICT FILTERING: Never expose acquirer or private data
    const publicUnits = units.map((u: any) => ({
      id: u.id,
      reference: u.reference,
      type_id: u.unit_type_id,
      floor: u.floor,
      surface: u.surface,
      price: u.price,
      parking_included: u.parking_option,
      status: u.status
    }));
    
    return sendSuccess(res, 200, 'Disponibilité des lots', publicUnits);
  } catch (error: any) {
    return sendError(res, 400, error.message);
  }
};

export const getProjectProgress = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const project = await residenceProjectsRepo.findBySlug(req.params.slug as string);
    if (!project) return sendError(res, 404, 'Projet introuvable');
    
    // Fetch milestones instead of a single progress field
    const milestones = await constructionRepo.findMilestonesByProject(project.id);
    return sendSuccess(res, 200, 'Étapes du projet', milestones);
  } catch (error: any) {
    return sendError(res, 400, error.message);
  }
};

export const getProjectUpdates = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const project = await residenceProjectsRepo.findBySlug(req.params.slug as string);
    if (!project) return sendError(res, 404, 'Projet introuvable');
    
    // Get public updates
    const updates = await constructionRepo.findUpdatesByProject(project.id);
    const publicUpdates = updates.filter((u: any) => u.status === 'PUBLISHED');
    
    return sendSuccess(res, 200, 'Actualités du chantier', publicUpdates);
  } catch (error: any) {
    return sendError(res, 400, error.message);
  }
};
