package com.printcalculator.controller.admin;

import com.printcalculator.dto.AdminOrderStatusUpdateRequest;
import com.printcalculator.dto.OrderDto;
import com.printcalculator.service.order.AdminOrderControllerService;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/api/admin/orders")
@Transactional(readOnly = true)
public class AdminOrderController {

    private final AdminOrderControllerService adminOrderControllerService;

    public AdminOrderController(AdminOrderControllerService adminOrderControllerService) {
        this.adminOrderControllerService = adminOrderControllerService;
    }

    @GetMapping
    public ResponseEntity<List<OrderDto>> listOrders() {
        return ResponseEntity.ok(adminOrderControllerService.listOrders());
    }

    @GetMapping("/{orderId}")
    public ResponseEntity<OrderDto> getOrder(@PathVariable UUID orderId) {
        return ResponseEntity.ok(adminOrderControllerService.getOrder(orderId));
    }

    @PostMapping("/{orderId}/payments/confirm")
    @Transactional
    public ResponseEntity<OrderDto> updatePaymentMethod(
            @PathVariable UUID orderId,
            @RequestBody(required = false) Map<String, String> payload
    ) {
        return ResponseEntity.ok(adminOrderControllerService.updatePaymentMethod(orderId, payload));
    }

    @PostMapping("/{orderId}/status")
    @Transactional
    public ResponseEntity<OrderDto> updateOrderStatus(
            @PathVariable UUID orderId,
            @RequestBody AdminOrderStatusUpdateRequest payload
    ) {
        return ResponseEntity.ok(adminOrderControllerService.updateOrderStatus(orderId, payload));
    }

    @GetMapping("/{orderId}/items/{orderItemId}/file")
    public ResponseEntity<Resource> downloadOrderItemFile(
            @PathVariable UUID orderId,
            @PathVariable UUID orderItemId
    ) {
        return adminOrderControllerService.downloadOrderItemFile(orderId, orderItemId);
    }

    @GetMapping("/{orderId}/documents/confirmation")
    public ResponseEntity<byte[]> downloadOrderConfirmation(@PathVariable UUID orderId) {
        return adminOrderControllerService.downloadOrderConfirmation(orderId);
    }

    @GetMapping("/{orderId}/documents/invoice")
    public ResponseEntity<byte[]> downloadOrderInvoice(@PathVariable UUID orderId) {
        return adminOrderControllerService.downloadOrderInvoice(orderId);
    }
}
