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

use app\adminapi\controller\BaseAdminController;
use app\service\domain\admin\system\CorePlatformService;
use app\model\AdminUser;
use app\validate\admin\PlatformMenuValidator;
use app\validate\domain\PlatformMenuDomainValidator;
use think\Request;


/**
 * 平台聚合控制器
 * 提供平台级管理能力：导航、菜单配置、行业管理、权限聚合、缓存同步
 * @package app\adminapi\controller\system
 */
class Platform extends BaseAdminController
{
    /**
     * 获取平台导航
     * @return \think\response\Json
     */
    public function navigation()
    {
        return json($this->service()->navigation($this->currentUser()));
    }

    /**
     * 获取平台菜单配置
     * @return \think\response\Json
     */
    public function menuConfig()
    {
        return json($this->service()->menuConfig());
    }

    /**
     * 获取平台应用列表
     * @return \think\response\Json
     */
    public function applications()
    {
        return json($this->service()->applications());
    }

    /**
     * 安装平台应用
     * @param Request $request
     * @return \think\response\Json
     */
    public function installApplication(Request $request)
    {
        $code = trim((string)$request->post('code', ''));

        return json($this->service()->installApplication($code));
    }

    /**
     * 卸载平台应用
     * @param Request $request
     * @return \think\response\Json
     */
    public function uninstallApplication(Request $request)
    {
        $code = trim((string)$request->post('code', ''));
        $dropTables = (bool)$request->post('drop_tables', false);

        return json($this->service()->uninstallApplication($code, $dropTables));
    }

    /**
     * 获取平台应用数据库表
     * @param Request $request
     * @return \think\response\Json
     */
    public function appTables(Request $request)
    {
        $code = trim((string)$request->get('code', ''));

        return json($this->service()->getApplicationTables($code));
    }

    /**
     * 切换平台应用状态
     * @param Request $request
     * @return \think\response\Json
     */
    public function toggleApplication(Request $request)
    {
        $code = trim((string)$request->post('code', ''));
        $enabled = (bool)$request->post('enabled', false);

        return json($this->service()->toggleApplication($code, $enabled));
    }

    /**
     * 获取平台应用插件
     * @param Request $request
     * @return \think\response\Json
     */
    public function appPlugins(Request $request)
    {
        $code = trim((string)$request->get('code', ''));

        return json($this->service()->getApplicationPlugins($code));
    }

    /**
     * 切换平台应用插件状态
     * @param Request $request
     * @return \think\response\Json
     */
    public function togglePlugin(Request $request)
    {
        $code = trim((string)$request->post('code', ''));
        $enabled = (bool)$request->post('enabled', false);

        return json($this->service()->togglePlugin($code, $enabled));
    }

    /**
     * 获取平台消息
     * @return \think\response\Json
     */
    public function messages()
    {
        return json($this->service()->messages());
    }

    /**
     * 获取平台设置
     * @return \think\response\Json
     */
    public function settings()
    {
        return json($this->service()->settings($this->currentUser()));
    }

    /**
     * 获取平台权限
     * @return \think\response\Json
     */
    public function permissions()
    {
        return json($this->service()->permissions());
    }

    /**
     * 同步平台缓存
     * @return \think\response\Json
     */
    public function sync()
    {
        return json($this->service()->sync());
    }

    /**
     * 保存平台菜单配置
     * @param Request $request
     * @return \think\response\Json
     */
    public function saveMenuConfig(Request $request)
    {
        $params = [
            'items' => $request->post('items', []),
        ];
        $this->validate($params, PlatformMenuValidator::class . '.save');
        $this->validateDomain($this->app->make(PlatformMenuDomainValidator::class), $params, 'save');

        return json($this->service()->saveMenuConfig($params['items']));
    }

    /**
     * 重置平台菜单配置
     * @return \think\response\Json
     */
    public function resetMenuConfig()
    {
        return json($this->service()->resetMenuConfig());
    }

    /**
     * 获取当前登录的管理员用户
     * @return AdminUser
     */
    private function currentUser(): AdminUser
    {
        return AdminUser::find((int)$this->request->uid) ?: new AdminUser();
    }

    /**
     * 获取平台核心服务实例
     * @return CorePlatformService
     */
    private function service(): CorePlatformService
    {
        return $this->app->make(CorePlatformService::class);
    }
}
