package com.zioinfo.kintex.work.schedule;

import com.zioinfo.kintex.auth.EventAccessGuard;
import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.common.ApiResponse;
import jakarta.validation.Valid;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/** 일정 API (/api/work/schedules). 인증 사용자. */
@RestController
@RequestMapping("/api/work/schedules")
public class ScheduleController {

    private final ScheduleService service;
    private final EventAccessGuard guard;

    public ScheduleController(ScheduleService service, EventAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    @GetMapping
    public ApiResponse<List<ScheduleDto>> list(@AuthenticationPrincipal KintexPrincipal principal,
                                               @RequestParam(required = false) String eventId,
                                               @RequestParam(required = false) String fromAt,
                                               @RequestParam(required = false) String toAt) {
        guard.require(principal);
        return ApiResponse.ok(service.list(principal, eventId, fromAt, toAt));
    }

    @GetMapping("/{id}")
    public ApiResponse<ScheduleDto> get(@AuthenticationPrincipal KintexPrincipal principal,
                                        @PathVariable String id) {
        guard.require(principal);
        return ApiResponse.ok(service.get(principal, id));
    }

    @PostMapping
    public ApiResponse<ScheduleDto> create(@AuthenticationPrincipal KintexPrincipal principal,
                                           @Valid @RequestBody ScheduleSaveRequest req) {
        guard.require(principal);
        return ApiResponse.ok(service.create(principal, req));
    }

    @PutMapping("/{id}")
    public ApiResponse<ScheduleDto> update(@AuthenticationPrincipal KintexPrincipal principal,
                                           @PathVariable String id,
                                           @Valid @RequestBody ScheduleSaveRequest req) {
        guard.require(principal);
        return ApiResponse.ok(service.update(principal, id, req));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@AuthenticationPrincipal KintexPrincipal principal,
                                    @PathVariable String id) {
        guard.require(principal);
        service.delete(principal, id);
        return ApiResponse.ok(null);
    }
}
