package com.flightticket.controller;

import com.flightticket.model.entity.RechargeRequest;
import com.flightticket.model.entity.Wallet;
import com.flightticket.model.entity.WalletTransaction;
import com.flightticket.security.annotation.RequiresFinanceUser;
import com.flightticket.security.annotation.RequiresSuperAdmin;
import com.flightticket.service.WalletService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/api/v1/wallet")
@RequiredArgsConstructor
public class WalletController {
    
    private final WalletService walletService;
    
    @GetMapping("/agency/{agencyId}")
    @RequiresFinanceUser
    public ResponseEntity<Wallet> getWalletByAgency(@PathVariable UUID agencyId) {
        log.info("Get wallet request for agency: {}", agencyId);
        Wallet wallet = walletService.getWalletByAgency(agencyId);
        return ResponseEntity.ok(wallet);
    }
    
    @PostMapping("/recharge")
    @RequiresFinanceUser
    public ResponseEntity<RechargeRequest> createRechargeRequest(
            @RequestParam UUID agencyId,
            @RequestParam BigDecimal amount,
            Authentication authentication) {
        log.info("Create recharge request for agency {} with amount {}", agencyId, amount);
        UUID requestedBy = UUID.fromString(authentication.getName());
        RechargeRequest request = walletService.createRechargeRequest(agencyId, amount, requestedBy);
        return ResponseEntity.status(HttpStatus.CREATED).body(request);
    }
    
    @PostMapping("/recharge/{requestId}/approve")
    @RequiresSuperAdmin
    public ResponseEntity<Void> approveRecharge(
            @PathVariable UUID requestId,
            Authentication authentication) {
        log.info("Approve recharge request: {}", requestId);
        UUID approvedBy = UUID.fromString(authentication.getName());
        walletService.approveRecharge(requestId, approvedBy);
        return ResponseEntity.ok().build();
    }
    
    @PostMapping("/recharge/{requestId}/reject")
    @RequiresSuperAdmin
    public ResponseEntity<Void> rejectRecharge(
            @PathVariable UUID requestId,
            @RequestParam String reason,
            Authentication authentication) {
        log.info("Reject recharge request: {} with reason: {}", requestId, reason);
        UUID rejectedBy = UUID.fromString(authentication.getName());
        walletService.rejectRecharge(requestId, rejectedBy, reason);
        return ResponseEntity.ok().build();
    }
    
    @GetMapping("/transactions/{agencyId}")
    @RequiresFinanceUser
    public ResponseEntity<List<WalletTransaction>> getTransactionHistory(
            @PathVariable UUID agencyId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate fromDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate toDate) {
        log.info("Get transaction history for agency {} from {} to {}", agencyId, fromDate, toDate);
        List<WalletTransaction> transactions = walletService.getTransactionHistory(agencyId, fromDate, toDate);
        return ResponseEntity.ok(transactions);
    }
}
