<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

/**
 * 微信发票服务类
 */
final class WechatInvoiceService
{
    /**
     * 构造函数
     */
    public function __construct(
        private readonly Container $app
    ) {}

    /**
     * 开具发票
     */
    public function createInvoice(array $params, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->invoice->create($params);

        return [
            'code' => 'SUCCESS',
            'message' => '开具发票成功',
            'data' => $result
        ];
    }

    /**
     * 查询发票
     */
    public function queryInvoice(string $invoiceId, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->invoice->query($invoiceId);

        return [
            'code' => 'SUCCESS',
            'message' => '查询发票成功',
            'data' => $result
        ];
    }

    /**
     * 红冲发票
     */
    public function redInvoice(string $invoiceId, string $reason, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->invoice->red($invoiceId, $reason);

        return [
            'code' => 'SUCCESS',
            'message' => '红冲发票成功',
            'data' => $result
        ];
    }

    /**
     * 获取电子发票列表
     */
    public function getInvoiceList(array $params, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->invoice->list($params);

        return [
            'code' => 'SUCCESS',
            'message' => '获取电子发票列表成功',
            'data' => $result
        ];
    }

    /**
     * 下载电子发票
     */
    public function downloadInvoice(string $invoiceId, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->invoice->download($invoiceId);

        return [
            'code' => 'SUCCESS',
            'message' => '下载电子发票成功',
            'data' => $result
        ];
    }

    /**
     * 获取发票授权
     */
    public function getInvoiceAuth(string $appId, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->invoice->getAuth($appId);

        return [
            'code' => 'SUCCESS',
            'message' => '获取发票授权成功',
            'data' => $result
        ];
    }

    /**
     * 刷新发票授权
     */
    public function refreshInvoiceAuth(string $appId, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->invoice->refreshAuth($appId);

        return [
            'code' => 'SUCCESS',
            'message' => '刷新发票授权成功',
            'data' => $result
        ];
    }

    /**
     * 获取微信应用实例
     */
    protected function getApp(string $driver, string $name): mixed
    {
        return $this->app->make('kylin.wechat')->driver($driver, $name);
    }
}
