package com.zioinfo.esn.uiws.schedule.controller;

import com.zioinfo.esn.common.ApiResponse;
import com.zioinfo.esn.uiws.schedule.dto.ScheduleDtos.*;
import com.zioinfo.esn.uiws.schedule.service.DiaryService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * UIWS 이식 — 일지(모듈 02). 인증 필수(/api/diaries).
 */
@RestController
@RequestMapping("/api/diaries")
@RequiredArgsConstructor
public class DiaryController {

    private final DiaryService diaryService;

    @GetMapping
    public ApiResponse<DiaryPage> list(
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate fromDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate toDate,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size) {
        return ApiResponse.ok(diaryService.list(fromDate, toDate, page, size));
    }

    @PostMapping
    public ApiResponse<DiaryDetailDto> create(@Valid @RequestBody DiarySaveDto dto) {
        return ApiResponse.ok(diaryService.create(dto));
    }

    @GetMapping("/{id}")
    public ApiResponse<DiaryDetailDto> detail(@PathVariable("id") Long id) {
        return ApiResponse.ok(diaryService.detail(id));
    }

    @PutMapping("/{id}")
    public ApiResponse<DiaryDetailDto> update(@PathVariable("id") Long id, @Valid @RequestBody DiarySaveDto dto) {
        return ApiResponse.ok(diaryService.update(id, dto));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable("id") Long id) {
        diaryService.delete(id);
        return ApiResponse.ok(null);
    }
}
