package com.example.demo.account;

import com.example.demo.common.AppDefaultsProperties;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/accounts")
public class AccountController {

    private final AccountService accountService;
    private final AppDefaultsProperties defaults;

    public AccountController(AccountService accountService, AppDefaultsProperties defaults) {
        this.accountService = accountService;
        this.defaults = defaults;
    }

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

    @PostMapping
    public ResponseEntity<?> create(@RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                    @RequestHeader(name = "X-User-Id", required = false) Long userId,
                                    @RequestBody AccountDtos.CreateAccountRequest req) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        Long uid = (userId == null ? defaults.getUserId() : userId);
        Long id = accountService.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 AccountDtos.CreateAccountRequest req) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        Long uid = (userId == null ? defaults.getUserId() : userId);
        accountService.update(id, sid, uid, req);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{id}/ledger")
    public ResponseEntity<?> ledger(@PathVariable("id") Long id,
                                    @RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                    @RequestParam(name = "startDate", required = false) String startDate,
                                    @RequestParam(name = "endDate", required = false) String endDate,
                                    @RequestParam(name = "kw", required = false) String kw,
                                    @RequestParam(name = "page", defaultValue = "1") int page,
                                    @RequestParam(name = "size", defaultValue = "20") int size) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        return ResponseEntity.ok(accountService.ledger(id, sid, kw, Math.max(0, page-1), size, startDate, endDate));
    }
}


