package kr.co.accura.wtm.api;

import jakarta.validation.Valid;
import kr.co.accura.wbx.spring.common.SecurityUtils;
import kr.co.accura.wtm.domain.timesheet.dto.*;
import kr.co.accura.wtm.domain.timesheet.service.TimesheetService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/api/wtm/timesheets")
@RequiredArgsConstructor
public class TimesheetController {

    private final TimesheetService timesheetService;

    @GetMapping("/week")
    public TimesheetDto getWeekly(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate weekStart) {
        return timesheetService.getOrCreateWeekly(
                SecurityUtils.getCurrentUserId(), weekStart);
    }

    @PostMapping("/{timesheetId}/entries")
    public TimesheetEntryDto saveEntry(
            @PathVariable Long timesheetId,
            @Valid @RequestBody TimesheetEntryRequest request) {
        return timesheetService.saveEntry(timesheetId, request);
    }

    @PutMapping("/{timesheetId}/entries/batch")
    public TimesheetDto saveBatch(
            @PathVariable Long timesheetId,
            @Valid @RequestBody List<TimesheetEntryRequest> entries) {
        return timesheetService.saveBatch(timesheetId, entries);
    }

    @DeleteMapping("/{timesheetId}/entries/{entryId}")
    public ResponseEntity<Void> deleteEntry(
            @PathVariable Long timesheetId,
            @PathVariable Long entryId) {
        timesheetService.deleteEntry(timesheetId, entryId);
        return ResponseEntity.noContent().build();
    }

    @PostMapping("/{timesheetId}/submit")
    public TimesheetDto submit(@PathVariable Long timesheetId) {
        return timesheetService.submit(timesheetId);
    }

    @PostMapping("/upload")
    public UploadResultDto uploadExcel(
            @RequestParam("file") MultipartFile file,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate weekStart) {
        return timesheetService.uploadExcel(
                SecurityUtils.getCurrentUserId(), file, weekStart);
    }

    @GetMapping("/upload/template")
    public ResponseEntity<Void> downloadTemplate() {
        // TODO: Generate and return empty Excel template for timesheet upload
        return ResponseEntity.status(501).build();
    }

    @GetMapping("/history")
    public Page<TimesheetDto> history(
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate from,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate to,
            Pageable pageable) {
        return timesheetService.getHistory(
                SecurityUtils.getCurrentUserId(), from, to, pageable);
    }
}
