package com.yaoai.admin.billing;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.yaoai.admin.audit.annotation.AdminAudited;
import com.yaoai.admin.auth.StpAdminUtil;
import com.yaoai.admin.billing.dto.BillingCostItem;
import com.yaoai.admin.billing.dto.BillingCostUpdateRequest;
import com.yaoai.admin.billing.dto.BillingRecordQuery;
import com.yaoai.admin.billing.dto.BillingSummaryItem;
import com.yaoai.admin.billing.dto.UsageDetailResponse;
import com.yaoai.admin.common.PageResult;
import com.yaoai.common.result.Result;
import com.yaoai.domain.entity.BillingRecord;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequiredArgsConstructor
@SaCheckLogin(type = "admin")
public class BillingAdminController {

    private final BillingAdminService billingAdminService;

    @GetMapping("/admin-api/billing/records")
    public Result<PageResult<BillingRecord>> records(@ModelAttribute BillingRecordQuery query) {
        return Result.ok(billingAdminService.records(query));
    }

    @GetMapping("/admin-api/billing/recharges")
    public Result<PageResult<BillingRecord>> recharges(@ModelAttribute BillingRecordQuery query) {
        return Result.ok(billingAdminService.recharges(query));
    }

    @GetMapping("/admin-api/billing/consumes")
    public Result<PageResult<BillingRecord>> consumes(@ModelAttribute BillingRecordQuery query) {
        return Result.ok(billingAdminService.consumes(query));
    }

    @GetMapping("/admin-api/billing/summary")
    public Result<List<BillingSummaryItem>> summary(
            @RequestParam(defaultValue = "modality") String dim,
            @ModelAttribute BillingRecordQuery query) {
        return Result.ok(billingAdminService.summary(dim, query));
    }

    @GetMapping("/admin-api/billing/costs")
    @SaCheckRole(value = "SUPER_ADMIN", type = "admin")
    public Result<List<BillingCostItem>> costs() {
        return Result.ok(billingAdminService.costs());
    }

    @PutMapping("/admin-api/billing/costs")
    @AdminAudited(action = "BILLING_COST_UPDATE", resource = "BILLING_COST")
    @SaCheckRole(value = "SUPER_ADMIN", type = "admin")
    public Result<List<BillingCostItem>> saveCosts(@RequestBody BillingCostUpdateRequest request) {
        return Result.ok(billingAdminService.saveCosts(request.getCosts(), StpAdminUtil.getLoginIdAsLong()));
    }

    @GetMapping("/admin-api/tenants/{id}/usage-detail")
    public Result<UsageDetailResponse> usageDetail(
            @PathVariable Long id,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime from,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime to,
            @RequestParam(defaultValue = "model") String groupBy) {
        return Result.ok(billingAdminService.usageDetail(id, from, to, groupBy));
    }
}
