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

import com.zioinfo.cms.common.ApiResponse;
import com.zioinfo.cms.uiws.schedule.dto.ScheduleDtos.*;
import com.zioinfo.cms.uiws.schedule.service.ScheduleService;
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;
import java.util.List;

/**
 * UIWS 이식 — 일정(모듈 02). 인증 필수(/api/schedules). 원본 엔드포인트 보존.
 */
@Tag(name = "UIWS-일정", description = "UIWS 이식: 개인/부서 일정")
@RestController
@RequestMapping("/api/schedules")
@RequiredArgsConstructor
public class ScheduleController {

    private final ScheduleService scheduleService;

    @Operation(summary = "개인/부서 일정(달력 month/week/day)")
    @GetMapping
    public ApiResponse<List<ScheduleDto>> calendar(
            @RequestParam(defaultValue = "PERSONAL") String type,
            @RequestParam(defaultValue = "month") String view,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate baseDate,
            @RequestParam(required = false) String deptId) {
        return ApiResponse.ok(scheduleService.calendar(type, view, baseDate, deptId));
    }

    @Operation(summary = "전체일정 목록")
    @GetMapping("/all")
    public ApiResponse<SchedulePage> all(
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate fromDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate toDate,
            @RequestParam(required = false) String type,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size) {
        return ApiResponse.ok(scheduleService.all(fromDate, toDate, type, page, size));
    }

    @Operation(summary = "일정검색 팝업")
    @GetMapping("/search")
    public ApiResponse<List<ScheduleDto>> search(@RequestParam(required = false) String keyword) {
        return ApiResponse.ok(scheduleService.search(keyword));
    }

    @Operation(summary = "일정 등록")
    @PostMapping
    public ApiResponse<ScheduleDto> create(@Valid @RequestBody ScheduleSaveDto dto) {
        return ApiResponse.ok(scheduleService.create(dto));
    }

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

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

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