<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

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

    /**
     * 上传临时素材
     */
    public function uploadTemporary(string $type, string $path, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->material->upload($type, $path);

        return [
            'code' => 'SUCCESS',
            'message' => '上传临时素材成功',
            'data' => $result
        ];
    }

    /**
     * 获取临时素材
     */
    public function getTemporary(string $mediaId, string $driver = 'official_account', string $name = 'default'): mixed
    {
        $app = $this->getApp($driver, $name);
        
        return $app->material->get($mediaId);
    }

    /**
     * 上传永久素材
     */
    public function uploadPermanent(string $type, string $path, array $options = [], string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->material->uploadMaterial($type, $path, $options);

        return [
            'code' => 'SUCCESS',
            'message' => '上传永久素材成功',
            'data' => $result
        ];
    }

    /**
     * 删除永久素材
     */
    public function deletePermanent(string $mediaId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->material->delete($mediaId);

        return [
            'code' => 'SUCCESS',
            'message' => '删除永久素材成功',
            'data' => $result
        ];
    }

    /**
     * 获取永久素材
     */
    public function getPermanent(string $mediaId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->material->getMaterial($mediaId);

        return [
            'code' => 'SUCCESS',
            'message' => '获取永久素材成功',
            'data' => $result
        ];
    }

    /**
     * 获取素材列表
     */
    public function getMaterialList(string $type, int $offset = 0, int $count = 20, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->material->list($type, $offset, $count);

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

    /**
     * 获取素材总数
     */
    public function getMaterialCount(string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->material->count();

        return [
            'code' => 'SUCCESS',
            'message' => '获取素材总数成功',
            'data' => $result
        ];
    }

    /**
     * 上传图文消息素材
     */
    public function uploadNews(array $articles, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->material->uploadArticle($articles);

        return [
            'code' => 'SUCCESS',
            'message' => '上传图文消息素材成功',
            'data' => $result
        ];
    }

    /**
     * 修改图文消息素材
     */
    public function updateNews(string $mediaId, array $articles, int $index = 0, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->material->updateArticle($mediaId, $articles, $index);

        return [
            'code' => 'SUCCESS',
            'message' => '修改图文消息素材成功',
            'data' => $result
        ];
    }

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