package kr.co.accura.wtm.api;

import kr.co.accura.wtm.domain.user.dto.*;
import kr.co.accura.wtm.domain.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/api/wtm/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @GetMapping
    public ResponseEntity<Map<String, Object>> list(Pageable pageable) {
        Page<UserDto> page = userService.findAll(pageable);
        return ResponseEntity.ok(Map.of(
                "items", page.getContent(),
                "total", page.getTotalElements()
        ));
    }

    @GetMapping("/{id}")
    public ResponseEntity<UserDto> getById(@PathVariable Long id) {
        return ResponseEntity.ok(userService.findById(id));
    }

    @PutMapping("/{id}")
    public ResponseEntity<UserDto> update(@PathVariable Long id,
                                          @RequestBody @Valid UserUpdateRequest request) {
        return ResponseEntity.ok(userService.update(id, request));
    }

    @PutMapping("/{id}/roles")
    public ResponseEntity<Void> assignRoles(@PathVariable Long id,
                                            @RequestBody @Valid RoleAssignRequest request) {
        userService.assignRoles(id, request);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/upload/internal")
    public ResponseEntity<Map<String, Object>> uploadInternal(@RequestParam("file") MultipartFile file) {
        // TODO: Implement Excel upload for internal employees
        return ResponseEntity.ok(Map.of("message", "Internal user upload not yet implemented"));
    }

    @PostMapping("/upload/subcontractor")
    public ResponseEntity<Map<String, Object>> uploadSubcontractor(@RequestParam("file") MultipartFile file) {
        // TODO: Implement Excel upload for subcontractors
        return ResponseEntity.ok(Map.of("message", "Subcontractor upload not yet implemented"));
    }

    @GetMapping("/upload/template")
    public ResponseEntity<Void> downloadTemplate() {
        // TODO: Implement template download
        return ResponseEntity.noContent().build();
    }
}
