import express from 'express';
import { z } from 'zod';
import {
  createProject,
  listProjects,
  getProject,
  updateProject,
  deleteProject
} from '../services/projectService.js';
import {
  createProjectSchema,
  updateProjectSchema,
  listProjectsSchema
} from '../validation/schemas.js';
import logger from '../utils/logger.js';

const router = express.Router();

// ============================================================================
// CREATE Project
// ============================================================================

router.post('/projects', async (req, res, next) => {
  try {
    const validated = createProjectSchema.parse(req.body);
    const project = await createProject(validated, 1); // owner_id: 1 (ParzivalTD)
    
    logger.info({
      message: 'Project created',
      request_id: req.id,
      project_id: project.id
    });
    
    res.status(201).json({
      status: 'success',
      data: project,
      meta: {
        timestamp: new Date().toISOString(),
        request_id: req.id
      }
    });
  } catch (error) {
    next(error);
  }
});

// ============================================================================
// LIST Projects
// ============================================================================

router.get('/projects', async (req, res, next) => {
  try {
    const validated = listProjectsSchema.parse(req.query);
    const { projects, total } = await listProjects(validated);
    
    res.json({
      status: 'success',
      data: projects,
      meta: {
        total,
        limit: validated.limit,
        offset: validated.offset,
        timestamp: new Date().toISOString(),
        request_id: req.id
      }
    });
  } catch (error) {
    next(error);
  }
});

// ============================================================================
// GET Project Detail
// ============================================================================

router.get('/projects/:id', async (req, res, next) => {
  try {
    const projectId = z.coerce.number().positive().parse(req.params.id);
    const project = await getProject(projectId);
    
    res.json({
      status: 'success',
      data: project,
      meta: {
        timestamp: new Date().toISOString(),
        request_id: req.id
      }
    });
  } catch (error) {
    next(error);
  }
});

// ============================================================================
// UPDATE Project
// ============================================================================

router.put('/projects/:id', async (req, res, next) => {
  try {
    const projectId = z.coerce.number().positive().parse(req.params.id);
    const validated = updateProjectSchema.parse(req.body);
    const project = await updateProject(projectId, validated);
    
    logger.info({
      message: 'Project updated',
      request_id: req.id,
      project_id: projectId
    });
    
    res.json({
      status: 'success',
      data: project,
      meta: {
        timestamp: new Date().toISOString(),
        request_id: req.id
      }
    });
  } catch (error) {
    next(error);
  }
});

// ============================================================================
// DELETE Project
// ============================================================================

router.delete('/projects/:id', async (req, res, next) => {
  try {
    const projectId = z.coerce.number().positive().parse(req.params.id);
    await deleteProject(projectId);
    
    logger.info({
      message: 'Project deleted',
      request_id: req.id,
      project_id: projectId
    });
    
    res.status(204).send();
  } catch (error) {
    next(error);
  }
});

export default router;
