<?php

namespace Kylin\Payment\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Kylin\Payment\Facades\Payment;
use Kylin\Payment\Models\PaymentCallbackLog;

/**
 * 支付仪表盘控制器
 */
class DashboardController extends Controller
{
    /**
     * 显示仪表盘
     */
    public function index(Request $request): \Illuminate\View\View
    {
        // 获取基础统计数据
        $basicStats = Payment::statistics()->getBasicStats();
        
        // 获取按支付方式统计数据
        $statsByGateway = Payment::statistics()->getStatsByGateway();
        
        // 获取按日期统计数据（最近30天）
        $endDate = date('Y-m-d');
        $startDate = date('Y-m-d', strtotime('-30 days'));
        $statsByDate = Payment::statistics()->getStatsByDate($startDate, $endDate);
        
        // 获取按状态统计数据
        $statsByStatus = Payment::statistics()->getStatsByStatus();
        
        // 获取top 10支付记录
        $topPayments = Payment::statistics()->getTopPayments();
        
        // 获取退款率
        $refundRate = Payment::statistics()->getRefundRate();
        
        return view('kylin-payment::dashboard.index', [
            'basicStats' => $basicStats,
            'statsByGateway' => $statsByGateway,
            'statsByDate' => $statsByDate,
            'statsByStatus' => $statsByStatus,
            'topPayments' => $topPayments,
            'refundRate' => $refundRate,
            'startDate' => $startDate,
            'endDate' => $endDate,
        ]);
    }

    /**
     * 获取统计数据（AJAX）
     */
    public function getStats(Request $request): \Illuminate\Http\JsonResponse
    {
        $startDate = $request->input('start_date', date('Y-m-d', strtotime('-30 days')));
        $endDate = $request->input('end_date', date('Y-m-d'));
        $type = $request->input('type', 'date');
        
        $data = match ($type) {
            'gateway' => Payment::statistics()->getStatsByGateway(),
            'status' => Payment::statistics()->getStatsByStatus(),
            'date' => Payment::statistics()->getStatsByDate($startDate, $endDate),
            default => Payment::statistics()->getStatsByDate($startDate, $endDate),
        };
        
        return response()->json([
            'success' => true,
            'data' => $data,
        ]);
    }

    /**
     * 获取支付记录列表
     */
    public function payments(Request $request): \Illuminate\View\View
    {
        $params = $request->all();
        $payments = Payment::paymentRecord()->getRecords($params);
        
        return view('kylin-payment::dashboard.payments', [
            'payments' => $payments,
            'params' => $params,
        ]);
    }

    /**
     * 获取回调日志列表
     */
    public function callbacks(Request $request): \Illuminate\View\View
    {
        $params = $request->all();
        $callbacks = Payment::callbackLog()->getLogs($params);
        
        return view('kylin-payment::dashboard.callbacks', [
            'callbacks' => $callbacks,
            'params' => $params,
        ]);
    }

    /**
     * 获取回调日志详情
     */
    public function callbackDetail(Request $request): \Illuminate\Http\JsonResponse
    {
        $id = $request->input('id');
        $callback = PaymentCallbackLog::find($id);
        
        if (!$callback) {
            return response()->json([
                'success' => false,
                'message' => __('kylin-payment::payment.errors.order_not_found'),
            ]);
        }
        
        return response()->json([
            'success' => true,
            'callback' => $callback,
        ]);
    }
}
