import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import { projectController } from '../controllers/projectController.js';
import { validateProjectCreate, validateProjectUpdate } from '../../common/middleware/validateProject.js';

interface ProjectParams {
  id: string;
}

export async function projectRoutes(fastify: FastifyInstance) {
  // 获取项目列表
  fastify.get('/projects', async (request: FastifyRequest, reply: FastifyReply) => {
    return projectController.getProjects(request, reply);
  });

  // 获取单个项目详情
  fastify.get<{ Params: ProjectParams }>(
    '/projects/:id',
    async (request: FastifyRequest<{ Params: ProjectParams }>, reply: FastifyReply) => {
      return projectController.getProjectById(request, reply);
    }
  );

  // 创建项目
  fastify.post(
    '/projects',
    {
      preHandler: validateProjectCreate,
    },
    async (request: FastifyRequest, reply: FastifyReply) => {
      return projectController.createProject(request, reply);
    }
  );

  // 更新项目
  fastify.put<{ Params: ProjectParams }>(
    '/projects/:id',
    {
      preHandler: validateProjectUpdate,
    },
    async (request: FastifyRequest<{ Params: ProjectParams }>, reply: FastifyReply) => {
      return projectController.updateProject(request, reply);
    }
  );

  // 删除项目
  fastify.delete<{ Params: ProjectParams }>(
    '/projects/:id',
    async (request: FastifyRequest<{ Params: ProjectParams }>, reply: FastifyReply) => {
      return projectController.deleteProject(request, reply);
    }
  );
}

