<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

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

    /**
     * 创建直播房间
     */
    public function createRoom(array $params, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->live->createRoom($params);

        return [
            'code' => 'SUCCESS',
            'message' => '创建直播房间成功',
            'data' => $result
        ];
    }

    /**
     * 获取直播房间列表
     */
    public function getRoomList(int $start = 0, int $limit = 10, int $status = 0, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->live->getRoomList($start, $limit, $status);

        return [
            'code' => 'SUCCESS',
            'message' => '获取直播房间列表成功',
            'data' => $result
        ];
    }

    /**
     * 获取直播房间详情
     */
    public function getRoomDetail(int $roomId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->live->getRoomDetail($roomId);

        return [
            'code' => 'SUCCESS',
            'message' => '获取直播房间详情成功',
            'data' => $result
        ];
    }

    /**
     * 修改直播房间
     */
    public function updateRoom(int $roomId, array $params, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->live->updateRoom($roomId, $params);

        return [
            'code' => 'SUCCESS',
            'message' => '修改直播房间成功',
            'data' => $result
        ];
    }

    /**
     * 删除直播房间
     */
    public function deleteRoom(int $roomId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->live->deleteRoom($roomId);

        return [
            'code' => 'SUCCESS',
            'message' => '删除直播房间成功',
            'data' => $result
        ];
    }

    /**
     * 开始直播
     */
    public function startLive(int $roomId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->live->start($roomId);

        return [
            'code' => 'SUCCESS',
            'message' => '开始直播成功',
            'data' => $result
        ];
    }

    /**
     * 结束直播
     */
    public function stopLive(int $roomId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->live->stop($roomId);

        return [
            'code' => 'SUCCESS',
            'message' => '结束直播成功',
            'data' => $result
        ];
    }

    /**
     * 直播推流地址
     */
    public function getPushUrl(int $roomId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->live->getPushUrl($roomId);

        return [
            'code' => 'SUCCESS',
            'message' => '获取直播推流地址成功',
            'data' => $result
        ];
    }

    /**
     * 获取直播间分享二维码
     */
    public function getShareQrCode(int $roomId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->live->getShareQrCode($roomId);

        return [
            'code' => 'SUCCESS',
            'message' => '获取直播间分享二维码成功',
            'data' => $result
        ];
    }

    /**
     * 获取直播间商品列表
     */
    public function getGoodsList(int $roomId, int $start = 0, int $limit = 10, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->live->getGoodsList($roomId, $start, $limit);

        return [
            'code' => 'SUCCESS',
            'message' => '获取直播间商品列表成功',
            'data' => $result
        ];
    }

    /**
     * 添加直播间商品
     */
    public function addGoods(int $roomId, array $goodsIds, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->live->addGoods($roomId, $goodsIds);

        return [
            'code' => 'SUCCESS',
            'message' => '添加直播间商品成功',
            'data' => $result
        ];
    }

    /**
     * 删除直播间商品
     */
    public function deleteGoods(int $roomId, array $goodsIds, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->live->deleteGoods($roomId, $goodsIds);

        return [
            'code' => 'SUCCESS',
            'message' => '删除直播间商品成功',
            'data' => $result
        ];
    }

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