<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

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

    /**
     * 获取广告账户信息
     */
    public function getAdvertiserInfo(string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->advertisement->getAdvertiserInfo();

        return [
            'code' => 'SUCCESS',
            'message' => '获取广告账户信息成功',
            'data' => $result
        ];
    }

    /**
     * 创建广告计划
     */
    public function createCampaign(array $params, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->advertisement->createCampaign($params);

        return [
            'code' => 'SUCCESS',
            'message' => '创建广告计划成功',
            'data' => $result
        ];
    }

    /**
     * 获取广告计划列表
     */
    public function getCampaignList(array $params, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->advertisement->getCampaignList($params);

        return [
            'code' => 'SUCCESS',
            'message' => '获取广告计划列表成功',
            'data' => $result
        ];
    }

    /**
     * 修改广告计划
     */
    public function updateCampaign(int $campaignId, array $params, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->advertisement->updateCampaign($campaignId, $params);

        return [
            'code' => 'SUCCESS',
            'message' => '修改广告计划成功',
            'data' => $result
        ];
    }

    /**
     * 删除广告计划
     */
    public function deleteCampaign(int $campaignId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->advertisement->deleteCampaign($campaignId);

        return [
            'code' => 'SUCCESS',
            'message' => '删除广告计划成功',
            'data' => $result
        ];
    }

    /**
     * 创建广告组
     */
    public function createAdgroup(array $params, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->advertisement->createAdgroup($params);

        return [
            'code' => 'SUCCESS',
            'message' => '创建广告组成功',
            'data' => $result
        ];
    }

    /**
     * 获取广告组列表
     */
    public function getAdgroupList(array $params, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->advertisement->getAdgroupList($params);

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

    /**
     * 修改广告组
     */
    public function updateAdgroup(int $adgroupId, array $params, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->advertisement->updateAdgroup($adgroupId, $params);

        return [
            'code' => 'SUCCESS',
            'message' => '修改广告组成功',
            'data' => $result
        ];
    }

    /**
     * 删除广告组
     */
    public function deleteAdgroup(int $adgroupId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->advertisement->deleteAdgroup($adgroupId);

        return [
            'code' => 'SUCCESS',
            'message' => '删除广告组成功',
            'data' => $result
        ];
    }

    /**
     * 创建广告创意
     */
    public function createAd(array $params, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->advertisement->createAd($params);

        return [
            'code' => 'SUCCESS',
            'message' => '创建广告创意成功',
            'data' => $result
        ];
    }

    /**
     * 获取广告创意列表
     */
    public function getAdList(array $params, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->advertisement->getAdList($params);

        return [
            'code' => 'SUCCESS',
            'message' => '获取广告创意列表成功',
            'data' => $result
        ];
    }

    /**
     * 修改广告创意
     */
    public function updateAd(int $adId, array $params, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->advertisement->updateAd($adId, $params);

        return [
            'code' => 'SUCCESS',
            'message' => '修改广告创意成功',
            'data' => $result
        ];
    }

    /**
     * 删除广告创意
     */
    public function deleteAd(int $adId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->advertisement->deleteAd($adId);

        return [
            'code' => 'SUCCESS',
            'message' => '删除广告创意成功',
            'data' => $result
        ];
    }

    /**
     * 获取广告数据
     */
    public function getAdData(array $params, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->advertisement->getAdData($params);

        return [
            'code' => 'SUCCESS',
            'message' => '获取广告数据成功',
            'data' => $result
        ];
    }

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