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

import com.zioinfo.hrm.common.ApiResponse;
import com.zioinfo.hrm.uiws.schedule.dto.ScheduleDtos.*;
import com.zioinfo.hrm.uiws.schedule.service.DiaryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
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).
 */
@Tag(name = "UIWS-일지", description = "UIWS 이식: 일지")
@RestController
@RequestMapping("/api/diaries")
@RequiredArgsConstructor
public class DiaryController {

    private final DiaryService diaryService;

    @Operation(summary = "일지 목록")
    @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));
    }

    @Operation(summary = "일지 등록")
    @PostMapping
    public ApiResponse<DiaryDetailDto> create(@Valid @RequestBody DiarySaveDto dto) {
        return ApiResponse.ok(diaryService.create(dto));
    }

    @Operation(summary = "일지 상세")
    @GetMapping("/{id}")
    public ApiResponse<DiaryDetailDto> detail(@PathVariable("id") Long id) {
        return ApiResponse.ok(diaryService.detail(id));
    }

    @Operation(summary = "일지 수정")
    @PutMapping("/{id}")
    public ApiResponse<DiaryDetailDto> update(@PathVariable("id") Long id, @Valid @RequestBody DiarySaveDto dto) {
        return ApiResponse.ok(diaryService.update(id, dto));
    }

    @Operation(summary = "일지 삭제")
    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable("id") Long id) {
        diaryService.delete(id);
        return ApiResponse.ok(null);
    }
}
