<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

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

    /**
     * 创建门店
     */
    public function createPoi(array $params, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->poi->create($params);

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

    /**
     * 获取门店列表
     */
    public function getPoiList(int $begin = 0, int $limit = 10, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->poi->list($begin, $limit);

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

    /**
     * 获取门店详情
     */
    public function getPoiDetail(string $poiId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->poi->get($poiId);

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

    /**
     * 修改门店信息
     */
    public function updatePoi(string $poiId, array $params, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->poi->update($poiId, $params);

        return [
            'code' => 'SUCCESS',
            'message' => '修改门店信息成功',
            'data' => $result
        ];
    }

    /**
     * 删除门店
     */
    public function deletePoi(string $poiId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->poi->delete($poiId);

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

    /**
     * 上传门店图片
     */
    public function uploadPoiImage(string $imagePath, string $type = '0', string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->poi->uploadImage($imagePath, $type);

        return [
            'code' => 'SUCCESS',
            'message' => '上传门店图片成功',
            'data' => $result
        ];
    }

    /**
     * 提交审核门店
     */
    public function submitPoi(string $poiId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->poi->submit($poiId);

        return [
            'code' => 'SUCCESS',
            'message' => '提交审核门店成功',
            'data' => $result
        ];
    }

    /**
     * 获取审核状态
     */
    public function getPoiAuditStatus(string $poiId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->poi->getAuditStatus($poiId);

        return [
            'code' => 'SUCCESS',
            'message' => '获取审核状态成功',
            'data' => $result
        ];
    }

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