package com.erp.controller;

import com.erp.dto.ApiResponse;
import com.erp.service.TaxInvoiceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/tax-invoice")
@RequiredArgsConstructor
@Slf4j
public class TaxInvoiceController {

    private final TaxInvoiceService taxInvoiceService;

    @GetMapping("/list")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getTaxInvoiceList(
            @RequestAttribute("company_code") String companyCode,
            @RequestParam Map<String, Object> params) {
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(taxInvoiceService.getTaxInvoiceList(params)));
    }

    @GetMapping("/stats/monthly")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getMonthlyStats(
            @RequestAttribute("company_code") String companyCode,
            @RequestParam Map<String, Object> params) {
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(taxInvoiceService.getMonthlyStats(params)));
    }

    @GetMapping("/stats/cost-type")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getCostTypeStats(
            @RequestAttribute("company_code") String companyCode,
            @RequestParam Map<String, Object> params) {
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(taxInvoiceService.getCostTypeStats(params)));
    }

    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getTaxInvoiceInfo(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(taxInvoiceService.getTaxInvoiceInfo(params)));
    }

    @PostMapping
    public ResponseEntity<ApiResponse<Map<String, Object>>> insertTaxInvoice(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        return ResponseEntity.status(201).body(ApiResponse.success(taxInvoiceService.insertTaxInvoice(body)));
    }

    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> updateTaxInvoice(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        body.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(taxInvoiceService.updateTaxInvoice(body)));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> deleteTaxInvoice(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(taxInvoiceService.deleteTaxInvoice(params)));
    }

    @PostMapping("/{id}/issue")
    public ResponseEntity<ApiResponse<Map<String, Object>>> issueTaxInvoice(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(taxInvoiceService.issueTaxInvoice(params)));
    }

    @PostMapping("/{id}/cancel")
    public ResponseEntity<ApiResponse<Map<String, Object>>> cancelTaxInvoice(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id,
            @RequestBody(required = false) Map<String, Object> body) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("id", id);
        if (body != null) params.putAll(body);
        return ResponseEntity.ok(ApiResponse.success(taxInvoiceService.cancelTaxInvoice(params)));
    }
}
