package com.nebula.erp.sales.controller;

import com.nebula.erp.sales.requestmodel.*;
import com.nebula.erp.sales.service.PaymentHistoryService;
import com.nebula.erp.sales.utility.ApiResponseStructure;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;


@RestController
@RequestMapping("/api/payment-history")
public class PaymentHistoryController {

        @Autowired
        private PaymentHistoryService service;

        @PostMapping("/credit")
        public ResponseEntity<?> addCredit(@RequestBody PaymentCreditRequest req) {
                return ResponseEntity.ok(
                                new ApiResponseStructure<>("success", 200, "Credit Added Successfully",
                                                service.addCredit(req)));
        }

        @PostMapping("/debit")
        public ResponseEntity<?> addDebit(@RequestBody PaymentDebitRequest req) {
                return ResponseEntity.ok(
                                new ApiResponseStructure<>("success", 200, "Debit added", service.addDebit(req)));
        }

        @GetMapping("/balance/{patientId}")
        public ResponseEntity<?> getBalance(@PathVariable String patientId) {
                return ResponseEntity.ok(
                                new ApiResponseStructure<>("success", 200, "Balance fetched",
                                                service.getBalance(patientId)));
        }

        @PostMapping("/refund")
        public ResponseEntity<?> refund(@RequestBody PaymentCreditRequest req) {

                if (req.getAmount() == null || req.getAmount() <= 0) {
                        return ResponseEntity.badRequest().body(
                                        new ApiResponseStructure<>("error", 400, "Refund amount must be greater than 0",
                                                        null));
                }

                // Convert refund to negative credit
                req.setAmount(-Math.abs(req.getAmount()));

                return ResponseEntity.ok(
                                new ApiResponseStructure<>(
                                                "success",
                                                200,
                                                "Refund processed",
                                                service.addRefund(req)));
        }

        @GetMapping("/export/advance-added/encounter/{encounterId}")
        public ResponseEntity<ApiResponseStructure<Map<String, Object>>> exportAdvanceByEncounter(
                        @PathVariable String encounterId) {

                Map<String, Object> data = service.exportAdvanceByEncounter(encounterId);

                return ResponseEntity.ok(
                                new ApiResponseStructure<>(
                                                "success",
                                                200,
                                                "Advance bill generated for encounter",
                                                data));
        }

        @GetMapping("/export/refund/{encounterId}")
        public ResponseEntity<ApiResponseStructure<Map<String, Object>>> exportRefund(
                        @PathVariable String encounterId) {

                Map<String, Object> data = service.exportRefundByEncounter(encounterId);

                return ResponseEntity.ok(
                                new ApiResponseStructure<>(
                                                "success",
                                                200,
                                                "Refund bill generated for encounter",
                                                data));
        }
}