<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace app\repository\channel;

use app\model\ChannelConfig;
use app\model\ChannelNotifyConfig;
use app\model\ChannelPaymentConfig;
use app\model\ChannelProfile;
use app\repository\BaseRepository;


/**
 * Channel Repository
 * @package app\repository\channel
 */
class ChannelRepository extends BaseRepository
{

    /**
     * List all channel profiles ordered by sort and id
     * @return array
     */
    public function listProfiles(): array
    {
        return ChannelProfile::order('sort', 'asc')->order('id', 'asc')->select()->toArray();
    }


    /**
     * 获取单个渠道资料
     * @param string $channel
     * @return array
     */
    public function getProfile(string $channel): array
    {
        $row = ChannelProfile::where('channel', $channel)->find();
        return is_array($row) ? $row : ($row ? $row->toArray() : []);
    }


    /**
     * Create or update a channel profile
     * @param string $channel Channel identifier
     * @param array $data Profile data
     * @return void
     */
    public function saveProfile(string $channel, array $data): void
    {
        $profile = ChannelProfile::where('channel', $channel)->find();
        if (!$profile) {
            $profile = new ChannelProfile();
            $profile->channel = $channel;
        }
        $profile->save($data);
    }


    /**
     * Get a channel config value by channel, group and key
     * @param string $channel Channel identifier
     * @param string $group Config group
     * @param string $key Config key (default 'default')
     * @return array
     */
    public function getConfig(string $channel, string $group, string $key = 'default'): array
    {
        $row = ChannelConfig::where([
            'channel' => $channel,
            'config_group' => $group,
            'config_key' => $key,
        ])->find();

        if (!$row) {
            return [];
        }

        $value = json_decode((string)($row->config_value ?? '{}'), true);
        return is_array($value) ? $value : [];
    }


    /**
     * Create or update a channel config entry
     * @param string $channel Channel identifier
     * @param string $group Config group
     * @param array $value Config value (JSON-encoded)
     * @param string $key Config key (default 'default')
     * @return void
     */
    public function saveConfig(string $channel, string $group, array $value, string $key = 'default'): void
    {
        $row = ChannelConfig::where([
            'channel' => $channel,
            'config_group' => $group,
            'config_key' => $key,
        ])->find();

        if (!$row) {
            $row = new ChannelConfig();
            $row->channel = $channel;
            $row->config_group = $group;
            $row->config_key = $key;
        }

        $row->config_value = json_encode($value, JSON_UNESCAPED_UNICODE);
        $row->save();
    }


    /**
     * List payment configs for a channel
     * @param string $channel Channel identifier
     * @return array
     */
    public function listPaymentConfigs(string $channel): array
    {
        return ChannelPaymentConfig::where('channel', $channel)->order('sort', 'asc')->order('id', 'asc')->select()->toArray();
    }


    /**
     * Create or update a payment provider config for a channel
     * @param string $channel Channel identifier
     * @param string $provider Payment provider key
     * @param array $data Config data (status, sort, config)
     * @return void
     */
    public function savePaymentConfig(string $channel, string $provider, array $data): void
    {
        $row = ChannelPaymentConfig::where([
            'channel' => $channel,
            'provider' => $provider,
        ])->find();

        if (!$row) {
            $row = new ChannelPaymentConfig();
            $row->channel = $channel;
            $row->provider = $provider;
        }

        $row->status = (int)($data['status'] ?? 0);
        $row->sort = (int)($data['sort'] ?? 0);
        $row->config_value = json_encode($data['config'] ?? [], JSON_UNESCAPED_UNICODE);
        $row->save();
    }


    /**
     * 获取通知配置列表
     * @param string $channel
     * @return array
     */
    public function listNotifyConfigs(string $channel): array
    {
        return ChannelNotifyConfig::where('channel', $channel)->order('sort', 'asc')->order('id', 'asc')->select()->toArray();
    }


    /**
     * Create or update a notification driver config for a channel
     * @param string $channel Channel identifier
     * @param string $driver Notification driver key
     * @param array $data Config data (status, sort, config)
     * @return void
     */
    public function saveNotifyConfig(string $channel, string $driver, array $data): void
    {
        $row = ChannelNotifyConfig::where([
            'channel' => $channel,
            'driver' => $driver,
        ])->find();

        if (!$row) {
            $row = new ChannelNotifyConfig();
            $row->channel = $channel;
            $row->driver = $driver;
        }

        $row->status = (int)($data['status'] ?? 0);
        $row->sort = (int)($data['sort'] ?? 0);
        $row->config_value = json_encode($data['config'] ?? [], JSON_UNESCAPED_UNICODE);
        $row->save();
    }
}