<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

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

    /**
     * 创建卡券
     */
    public function create(array $card, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->card->create($card);

        return [
            'code' => 'SUCCESS',
            'message' => '创建卡券成功',
            'data' => $result
        ];
    }

    /**
     * 获取卡券详情
     */
    public function get(string $cardId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->card->get($cardId);

        return [
            'code' => 'SUCCESS',
            'message' => '获取卡券详情成功',
            'data' => $result
        ];
    }

    /**
     * 修改卡券
     */
    public function update(string $cardId, array $card, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->card->update($cardId, $card);

        return [
            'code' => 'SUCCESS',
            'message' => '修改卡券成功',
            'data' => $result
        ];
    }

    /**
     * 删除卡券
     */
    public function delete(string $cardId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->card->delete($cardId);

        return [
            'code' => 'SUCCESS',
            'message' => '删除卡券成功',
            'data' => $result
        ];
    }

    /**
     * 获取卡券列表
     */
    public function list(int $offset = 0, int $count = 10, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->card->list($offset, $count);

        return [
            'code' => 'SUCCESS',
            'message' => '获取卡券列表成功',
            'data' => $result
        ];
    }

    /**
     * 批量获取卡券详情
     */
    public function batchGet(array $cardIds, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->card->batchGet($cardIds);

        return [
            'code' => 'SUCCESS',
            'message' => '批量获取卡券详情成功',
            'data' => $result
        ];
    }

    /**
     * 激活卡券
     */
    public function activate(string $cardId, string $code, array $data = [], string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->card->activate($cardId, $code, $data);

        return [
            'code' => 'SUCCESS',
            'message' => '激活卡券成功',
            'data' => $result
        ];
    }

    /**
     * 核销卡券
     */
    public function consume(string $code, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->card->consume($code);

        return [
            'code' => 'SUCCESS',
            'message' => '核销卡券成功',
            'data' => $result
        ];
    }

    /**
     * 查询卡券code
     */
    public function getCode(string $code, string $cardId = '', string $checkConsume = 'false', string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->card->code->get($code, $cardId, $checkConsume);

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

    /**
     * 更改卡券code
     */
    public function updateCode(string $code, string $newCode, string $cardId = '', string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->card->code->update($code, $newCode, $cardId);

        return [
            'code' => 'SUCCESS',
            'message' => '更改卡券code成功',
            'data' => $result
        ];
    }

    /**
     * 微信卡券投放
     */
    public function createQRCode(array $card, int $expireSeconds = 60, bool $isUniqueCode = false, string $outerStr = '', string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->card->qrcode->create($card, $expireSeconds, $isUniqueCode, $outerStr);

        return [
            'code' => 'SUCCESS',
            'message' => '微信卡券投放成功',
            'data' => $result
        ];
    }

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