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

use app\service\api\BaseApiService;
use app\service\domain\channel\CoreBottomNavService;
use app\service\domain\channel\CoreChannelService;
use think\Request;


/**
 * 渠道服务
 * @package app\service\api\channel
 */
class ChannelService extends BaseApiService
{
    /**
     * 获取渠道列表
     * @return array
     */
    public function list(): array
    {
        $rows = $this->core()->list();
        $items = [];
        foreach ($rows as $row) {
            if ((int)($row['status'] ?? 0) !== 1) {
                continue;
            }
            $items[] = [
                'key' => (string)($row['key'] ?? ''),
                'name' => (string)($row['name'] ?? ''),
                'scene' => (string)($row['scene'] ?? ''),
            ];
        }

        return ['code' => 0, 'data' => $items];
    }


    /**
     * 获取渠道详情
     * @param string $channel 渠道键
     * @return array
     */
    public function detail(string $channel): array
    {
        $detail = $this->core()->detail($channel);
        if (!$detail || (int)($detail['profile']['status'] ?? 0) !== 1) {
            return ['code' => 1, 'msg' => ''];
        }

        return [
            'code' => 0,
            'data' => [
                'profile' => $detail['profile'],
                'payments' => array_map(static function (array $item): array {
                    return [
                        'key' => (string)($item['key'] ?? ''),
                        'name' => (string)($item['name'] ?? ''),
                        'status' => (int)($item['status'] ?? 0),
                    ];
                }, is_array($detail['payments'] ?? null) ? $detail['payments'] : []),
                'notifies' => array_map(static function (array $item): array {
                    return [
                        'key' => (string)($item['key'] ?? ''),
                        'name' => (string)($item['name'] ?? ''),
                        'status' => (int)($item['status'] ?? 0),
                    ];
                }, is_array($detail['notifies'] ?? null) ? $detail['notifies'] : []),
            ],
        ];
    }

    /**
     * 获取渠道底部导航配置
     * @param Request $request 请求对象
     * @return array array
     */
    public function bottomNavConfig(Request $request): array
    {
        $channel = trim((string)$request->get('channel', ''));
        $menuId = (int)$request->get('id', 0);
        $menus = $this->bottomNav()->publicMenus($request, $channel, $menuId);

        return [
            'code' => 0,
            'data' => [
                'menus' => $menus,
            ],
        ];
    }

    /**
     * 获取核心渠道服务
     * @return CoreChannelService
     */
    private function core(): CoreChannelService
    {
        return $this->app->make(CoreChannelService::class);
    }

    /**
     * 获取核心底部导航服务
     * @return CoreBottomNavService
     */
    private function bottomNav(): CoreBottomNavService
    {
        return $this->app->make(CoreBottomNavService::class);
    }
}