<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

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

    /**
     * 发送客服消息
     */
    public function send(array $message, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->customer_service->message($message)->send();

        return [
            'code' => 'SUCCESS',
            'message' => '发送客服消息成功',
            'data' => $result
        ];
    }

    /**
     * 发送文本消息
     */
    public function sendText(string $openId, string $content, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->customer_service->message()
            ->to($openId)
            ->content($content)
            ->send();

        return [
            'code' => 'SUCCESS',
            'message' => '发送文本消息成功',
            'data' => $result
        ];
    }

    /**
     * 发送图片消息
     */
    public function sendImage(string $openId, string $mediaId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->customer_service->message()
            ->to($openId)
            ->image($mediaId)
            ->send();

        return [
            'code' => 'SUCCESS',
            'message' => '发送图片消息成功',
            'data' => $result
        ];
    }

    /**
     * 发送语音消息
     */
    public function sendVoice(string $openId, string $mediaId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->customer_service->message()
            ->to($openId)
            ->voice($mediaId)
            ->send();

        return [
            'code' => 'SUCCESS',
            'message' => '发送语音消息成功',
            'data' => $result
        ];
    }

    /**
     * 发送视频消息
     */
    public function sendVideo(string $openId, string $mediaId, string $title, string $description, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->customer_service->message()
            ->to($openId)
            ->video($mediaId, $title, $description)
            ->send();

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

    /**
     * 发送图文消息
     */
    public function sendNews(string $openId, array $articles, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->customer_service->message()
            ->to($openId)
            ->news($articles)
            ->send();

        return [
            'code' => 'SUCCESS',
            'message' => '发送图文消息成功',
            'data' => $result
        ];
    }

    /**
     * 发送小程序卡片
     */
    public function sendMiniProgramPage(string $openId, string $title, string $pagePath, string $thumbMediaId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->customer_service->message()
            ->to($openId)
            ->miniProgramPage($title, $pagePath, $thumbMediaId)
            ->send();

        return [
            'code' => 'SUCCESS',
            'message' => '发送小程序卡片成功',
            'data' => $result
        ];
    }

    /**
     * 获取客服列表
     */
    public function getList(string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->customer_service->list();

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

    /**
     * 添加客服账号
     */
    public function add(string $kfAccount, string $nickname, string $password, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->customer_service->create($kfAccount, $nickname, $password);

        return [
            'code' => 'SUCCESS',
            'message' => '添加客服账号成功',
            'data' => $result
        ];
    }

    /**
     * 修改客服账号
     */
    public function update(string $kfAccount, string $nickname, string $password, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->customer_service->update($kfAccount, $nickname, $password);

        return [
            'code' => 'SUCCESS',
            'message' => '修改客服账号成功',
            'data' => $result
        ];
    }

    /**
     * 删除客服账号
     */
    public function delete(string $kfAccount, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->customer_service->delete($kfAccount);

        return [
            'code' => 'SUCCESS',
            'message' => '删除客服账号成功',
            'data' => $result
        ];
    }

    /**
     * 上传客服头像
     */
    public function uploadAvatar(string $kfAccount, string $path, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->customer_service->uploadAvatar($kfAccount, $path);

        return [
            'code' => 'SUCCESS',
            'message' => '上传客服头像成功',
            'data' => $result
        ];
    }

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