package com.example.demo.customer.controller;

import com.example.demo.common.AppDefaultsProperties;
import com.example.demo.customer.dto.CustomerDtos;
import com.example.demo.customer.entity.Customer;
import com.example.demo.customer.service.CustomerService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/customers")
public class CustomerController {

    private final CustomerService customerService;
    private final AppDefaultsProperties defaults;

    public CustomerController(CustomerService customerService, AppDefaultsProperties defaults) {
        this.customerService = customerService;
        this.defaults = defaults;
    }

    @GetMapping
    public ResponseEntity<?> search(@RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                    @RequestParam(name = "kw", required = false) String kw,
                                    @RequestParam(name = "debtOnly", required = false, defaultValue = "false") boolean debtOnly,
                                    @RequestParam(name = "page", defaultValue = "1") int page,
                                    @RequestParam(name = "size", defaultValue = "50") int size) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        return ResponseEntity.ok(customerService.search(sid, kw, debtOnly, Math.max(0, page-1), size));
    }

    @GetMapping("/{id}")
    public ResponseEntity<?> detail(@PathVariable("id") Long id,
                                    @RequestHeader(name = "X-Shop-Id", required = false) Long shopId) {
        java.util.Optional<Customer> oc = customerService.findById(id);
        if (oc.isEmpty()) return ResponseEntity.notFound().build();
        Customer c = oc.get();
        java.util.Map<String,Object> body = new java.util.HashMap<>();
        body.put("id", c.getId());
        body.put("name", c.getName());
        body.put("contactName", c.getContactName());
        body.put("mobile", c.getMobile());
        body.put("phone", c.getPhone());
        body.put("level", c.getLevel());
        body.put("priceLevel", c.getPriceLevel());
        body.put("remark", c.getRemark());
        body.put("address", c.getAddress());
        body.put("arOpening", c.getArOpening());
        Long sid = (shopId == null ? null : shopId);
        if (sid != null) {
            body.put("receivable", customerService.computeReceivable(sid, id));
        }
        return ResponseEntity.ok(body);
    }

    @PostMapping
    public ResponseEntity<?> create(@RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                    @RequestHeader(name = "X-User-Id", required = false) Long userId,
                                    @RequestBody CustomerDtos.CreateOrUpdateCustomerRequest req) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        Long uid = (userId == null ? defaults.getUserId() : userId);
        Long id = customerService.create(sid, uid, req);
        java.util.Map<String,Object> body = new java.util.HashMap<>(); body.put("id", id); return ResponseEntity.ok(body);
    }

    @PutMapping("/{id}")
    public ResponseEntity<?> update(@PathVariable("id") Long id,
                                    @RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                    @RequestHeader(name = "X-User-Id", required = false) Long userId,
                                    @RequestBody CustomerDtos.CreateOrUpdateCustomerRequest req) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        Long uid = (userId == null ? defaults.getUserId() : userId);
        customerService.update(id, sid, uid, req);
        return ResponseEntity.ok().build();
    }
}


