package com.pressing.modules.operations;

import jakarta.validation.Valid;
import java.time.LocalDate;

import com.pressing.common.dto.PageResponse;
import com.pressing.modules.catalog.dto.CatalogDtos.LocationRequest;
import com.pressing.modules.catalog.dto.CatalogDtos.LocationResponse;
import com.pressing.modules.operations.OperationsService.WalletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/api/v1")
public class OperationsController {

    private final OperationsService operationsService;

    public OperationsController(OperationsService operationsService) {
        this.operationsService = operationsService;
    }

    @PostMapping("/pickups")
    public Object pickup(@Valid @RequestBody OperationsService.PickupRequest req) {
        return operationsService.createPickup(req);
    }

    @GetMapping("/pickups")
    public Object pickupHistory(
            @RequestParam(required = false) String q,
            @RequestParam(required = false) LocalDate from,
            @RequestParam(required = false) LocalDate to,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "50") int size
    ) {
        return operationsService.pickupHistory(q, from, to, page, size);
    }


    @GetMapping("/pickups/invoice/{number}")
    public Object getInvoiceForPickup(@PathVariable String number) {
        return operationsService.getInvoiceForPickup(number);
    }


    @GetMapping("/ironing-invoices/{id}")
public Object getIroning(@PathVariable Long id) {
    return operationsService.getIroning(id);
}

@DeleteMapping("/ironing-invoices/{id}")
@PreAuthorize("hasAnyRole('ADMIN','MANAGER')")
public void deleteIroning(@PathVariable Long id) {
    operationsService.deleteIroning(id);
}


    @PostMapping("/ironing-invoices")
    public Object createIroning(@Valid @RequestBody OperationsService.IroningRequest req) {
        return operationsService.createIroning(req);
    }

    @PutMapping("/ironing-invoices/{id}")
    public Object updateIroning(@PathVariable Long id, @Valid @RequestBody OperationsService.IroningRequest req) {
        return operationsService.updateIroning(id, req);
    }

    @GetMapping("/ironing-invoices")
    public Object ironing(
            @RequestParam(required = false) String q,
            @RequestParam(required = false) LocalDate from,
            @RequestParam(required = false) LocalDate to,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "50") int size
    ) {
        return operationsService.ironingHistory(q, from, to, page, size);
    }

    @PostMapping("/payments")
    public Object pay(@Valid @RequestBody OperationsService.PaymentRequest req) {
        return operationsService.createPayment(req);
    }

    @GetMapping("/payments")
    public Object payments(
            @RequestParam(required = false) String q,
            @RequestParam(required = false) LocalDate from,
            @RequestParam(required = false) LocalDate to,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "50") int size
    ) {
        return operationsService.payments(q, from, to, page, size);
    }


@GetMapping("/payments/{id}/receipt")
public Object getPaymentReceipt(@PathVariable Long id) {
    return operationsService.getPaymentReceipt(id);
}

@GetMapping("/payments/search")
public Object searchInvoicesForPayment(@RequestParam String q) {
    return operationsService.searchInvoicesForPayment(q);
}

@PutMapping("/payments/{id}")
@PreAuthorize("hasAnyRole('ADMIN','MANAGER')")
public Object updatePayment(@PathVariable Long id, @Valid @RequestBody OperationsService.PaymentUpdateRequest req) {
    return operationsService.updatePayment(id, req);
}

@DeleteMapping("/payments/{id}")
@PreAuthorize("hasAnyRole('ADMIN','MANAGER')")
public void deletePayment(@PathVariable Long id) {
    operationsService.deletePayment(id);
}

    @PostMapping("/wallet-entries")
public WalletResponse createWallet(@Valid @RequestBody OperationsService.WalletRequest req) {
    return operationsService.createWallet(req);
}

@PutMapping("/wallet-entries/{id}")
public WalletResponse updateWallet(@PathVariable Long id, @Valid @RequestBody OperationsService.WalletRequest req) {
    return operationsService.updateWallet(id, req);
}

@DeleteMapping("/wallet-entries/{id}")
@PreAuthorize("hasAnyRole('ADMIN','MANAGER')")
public void deleteWallet(@PathVariable Long id) {
    operationsService.deleteWallet(id);
}

@GetMapping("/wallet-entries")
public PageResponse<WalletResponse> wallets(
        @RequestParam(required = false) String q,
        @RequestParam(defaultValue = "0") int page,
        @RequestParam(defaultValue = "50") int size
) {
    return operationsService.wallets(q, page, size);
}

    // Assignation d'un emplacement à une facture ou ligne
@PostMapping("/storage/assign")
public void assignStorage(@Valid @RequestBody OperationsService.StorageRequest req) {
    operationsService.assignStorage(req);
}




}
