import { Request, Response } from "express";
import { SampleService } from "../services/sampleService";

const sampleService = SampleService.getInstance();

/**
 * 목록 조회
 * GET /api/sample/list?page=1&limit=20
 */
export const getList = async (req: Request, res: Response): Promise<void> => {
  const page = Math.max(1, parseInt(req.query.page as string) || 1);
  const limit = Math.min(100, Math.max(1, parseInt(req.query.limit as string) || 20));
  const result = await sampleService.getList(page, limit);
  res.status(200).json({
    success: true,
    data: result.items,
    total: result.total,
    page,
    limit,
  });
};

/**
 * 등록
 * POST /api/sample
 */
export const create = async (req: Request, res: Response): Promise<void> => {
  const { name, description } = req.body;
  if (!name || !description) {
    res.status(400).json({ success: false, message: "name, description은 필수입니다." });
    return;
  }
  const item = await sampleService.create(name, description);
  res.status(201).json({ success: true, data: item });
};

/**
 * 수정
 * PUT /api/sample/:id
 */
export const update = async (req: Request, res: Response): Promise<void> => {
  const { id } = req.params;
  const { name, description } = req.body;
  const item = await sampleService.update(id, name, description);
  res.status(200).json({ success: true, data: item });
};

/**
 * 삭제 (soft delete)
 * DELETE /api/sample/:id
 */
export const remove = async (req: Request, res: Response): Promise<void> => {
  const { id } = req.params;
  await sampleService.softDelete(id);
  res.status(200).json({ success: true, message: "삭제되었습니다." });
};
