<?php

namespace Kylin\Payment\Http\Controllers;

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

/**
 * 支付报表控制器
 */
class ReportController extends Controller
{
    /**
     * 显示报表页面
     */
    public function index(Request $request): \Illuminate\View\View
    {
        // 获取默认日期范围
        $endDate = date('Y-m-d');
        $startDate = date('Y-m-d', strtotime('-30 days'));

        // 获取支付方式列表
        $gateways = [
            'wechat' => '微信支付',
            'alipay' => '支付宝',
            'unionpay' => '银联支付',
            'douyin' => '抖音支付',
            'jsbank' => '江苏银行',
            'alipay_global' => '支付宝国际版',
            'paypal' => 'PayPal',
            'stripe' => 'Stripe',
            'google_pay' => 'Google Pay',
            'apple_pay' => 'Apple Pay',
        ];

        // 获取支付状态列表
        $statuses = [
            'pending' => '待支付',
            'success' => '支付成功',
            'failed' => '支付失败',
            'refunded' => '已退款',
            'closed' => '已关闭',
        ];

        return view('kylin-payment::report.index', [
            'startDate' => $startDate,
            'endDate' => $endDate,
            'gateways' => $gateways,
            'statuses' => $statuses,
        ]);
    }

    /**
     * 生成报表数据
     */
    public function generate(Request $request): \Illuminate\Http\JsonResponse
    {
        $params = $request->only(['start_date', 'end_date', 'gateway', 'status']);

        try {
            $reportData = Payment::report()->generateReportData($params);

            return response()->json([
                'success' => true,
                'data' => $reportData,
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => $e->getMessage(),
            ]);
        }
    }

    /**
     * 导出Excel报表
     */
    public function exportExcel(Request $request): \Illuminate\Http\Response
    {
        $params = $request->only(['start_date', 'end_date', 'gateway', 'status']);

        try {
            $filename = Payment::report()->exportExcel($params);

            return response()->download(storage_path('app/' . $filename))->deleteFileAfterSend(true);
        } catch (\Exception $e) {
            return redirect()->back()->with('error', $e->getMessage());
        }
    }

    /**
     * 导出PDF报表
     */
    public function exportPdf(Request $request): \Illuminate\Http\Response
    {
        $params = $request->only(['start_date', 'end_date', 'gateway', 'status']);

        try {
            $filename = Payment::report()->exportPdf($params);

            return response()->download(storage_path('app/' . $filename))->deleteFileAfterSend(true);
        } catch (\Exception $e) {
            return redirect()->back()->with('error', $e->getMessage());
        }
    }

    /**
     * 导出CSV报表
     */
    public function exportCsv(Request $request): \Illuminate\Http\Response
    {
        $params = $request->only(['start_date', 'end_date', 'gateway', 'status']);

        try {
            $filename = Payment::report()->exportCsv($params);

            return response()->download(storage_path('app/' . $filename))->deleteFileAfterSend(true);
        } catch (\Exception $e) {
            return redirect()->back()->with('error', $e->getMessage());
        }
    }
}
