<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

/**
 * 微信视频号服务类
 */
final class WechatVideoAccountService
{
    /**
     * 构造函数
     */
    public function __construct(
        private readonly Container $app
    ) {}

    /**
     * 获取视频号信息
     */
    public function getAccountInfo(string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->video_account->info();

        return [
            'code' => 'SUCCESS',
            'message' => '获取视频号信息成功',
            'data' => $result
        ];
    }

    /**
     * 发布视频
     */
    public function publishVideo(array $params, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->video_account->publish($params);

        return [
            'code' => 'SUCCESS',
            'message' => '发布视频成功',
            'data' => $result
        ];
    }

    /**
     * 获取视频列表
     */
    public function getVideoList(int $page = 1, int $size = 10, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->video_account->list($page, $size);

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

    /**
     * 获取视频详情
     */
    public function getVideoDetail(string $videoId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->video_account->get($videoId);

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

    /**
     * 删除视频
     */
    public function deleteVideo(string $videoId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->video_account->delete($videoId);

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

    /**
     * 更新视频信息
     */
    public function updateVideo(string $videoId, array $params, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->video_account->update($videoId, $params);

        return [
            'code' => 'SUCCESS',
            'message' => '更新视频信息成功',
            'data' => $result
        ];
    }

    /**
     * 获取视频统计数据
     */
    public function getVideoStats(string $videoId, string $startDate, string $endDate, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->video_account->stats($videoId, $startDate, $endDate);

        return [
            'code' => 'SUCCESS',
            'message' => '获取视频统计数据成功',
            'data' => $result
        ];
    }

    /**
     * 获取视频号粉丝列表
     */
    public function getFansList(int $page = 1, int $size = 10, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->video_account->fansList($page, $size);

        return [
            'code' => 'SUCCESS',
            'message' => '获取视频号粉丝列表成功',
            'data' => $result
        ];
    }

    /**
     * 获取视频号评论列表
     */
    public function getCommentList(string $videoId, int $page = 1, int $size = 10, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->video_account->commentList($videoId, $page, $size);

        return [
            'code' => 'SUCCESS',
            'message' => '获取视频号评论列表成功',
            'data' => $result
        ];
    }

    /**
     * 回复视频评论
     */
    public function replyComment(string $videoId, string $commentId, string $content, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->video_account->replyComment($videoId, $commentId, $content);

        return [
            'code' => 'SUCCESS',
            'message' => '回复视频评论成功',
            'data' => $result
        ];
    }

    /**
     * 删除视频评论
     */
    public function deleteComment(string $videoId, string $commentId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->video_account->deleteComment($videoId, $commentId);

        return [
            'code' => 'SUCCESS',
            'message' => '删除视频评论成功',
            'data' => $result
        ];
    }

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