<?php

declare(strict_types=1);

namespace app\admin\controller\auth;

use Throwable;
use ba\Tree;
use app\common\library\Menu;
use app\admin\model\AdminRule;
use app\admin\model\AdminGroup;
use app\admin\library\crud\Helper;
use app\common\controller\Backend;
use support\Response;
use Webman\Http\Request;

class Rule extends Backend
{
    protected string|array $preExcludeFields = ['create_time', 'update_time'];

    protected string|array $defaultSortField = ['weigh' => 'desc'];

    protected string|array $quickSearchField = 'title';

    protected ?object $model = null;

    protected Tree $tree;

    protected array $initValue = [];

    protected string $keyword = '';

    protected bool $assembleTree = true;

    protected bool $modelValidate = false;

    protected function initController(Request $request): ?Response
    {
        $this->model     = new AdminRule();
        $this->tree      = Tree::instance();
        $isTree          = $request->get('isTree') ?? $request->post('isTree') ?? true;
        $initValue       = $request->get('initValue') ?? $request->post('initValue') ?? [];
        $this->initValue = is_array($initValue) ? array_filter($initValue) : [];
        $this->keyword   = $request->get('quickSearch') ?? $request->post('quickSearch') ?? '';
        $this->assembleTree = $isTree && !$this->initValue;
        return null;
    }

    public function index(Request $request): Response
    {
        $response = $this->initializeBackend($request);
        if ($response !== null) return $response;

        if ($request->get('select') ?? $request->post('select')) {
            return $this->select($request);
        }

        return $this->success('', [
            'list'   => $this->getMenus($request),
            'remark' => get_route_remark(),
        ]);
    }

    public function add(Request $request): Response
    {
        $response = $this->initializeBackend($request);
        if ($response !== null) return $response;

        if ($request->method() === 'POST') {
            $data = $request->post();
            if (!$data) {
                return $this->error(__('Parameter %s can not be empty', ['']));
            }

            $data = $this->excludeFields($data);
            if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                $data[$this->dataLimitField] = $this->auth->id;
            }

            $result = false;
            $this->model->startTrans();
            try {
                if ($this->modelValidate) {
                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    if (class_exists($validate)) {
                        $validate = new $validate();
                        if ($this->modelSceneValidate) {
                            $validate->scene('add');
                        }
                        $validate->check($data);
                    }
                }
                $result = $this->model->save($data);

                if (!empty($data['pid'])) {
                    $this->autoAssignPermission($this->model->id, (int) $data['pid']);
                }

                if (($data['type'] ?? '') == 'menu' && !empty($data['buttons'])) {
                    $newButtons = [];
                    foreach ($data['buttons'] as $button) {
                        foreach (Helper::$menuChildren as $menuChild) {
                            if ($menuChild['name'] == '/' . $button) {
                                $menuChild['name'] = $data['name'] . $menuChild['name'];
                                $newButtons[]      = $menuChild;
                            }
                        }
                    }
                    if (!empty($newButtons)) {
                        Menu::create($newButtons, $this->model->id, 'ignore');

                        $children = AdminRule::where('pid', $this->model->id)->select();
                        foreach ($children as $child) {
                            $this->autoAssignPermission($child['id'], $this->model->id);
                        }
                    }
                }

                $this->model->commit();
            } catch (Throwable $e) {
                $this->model->rollback();
                return $this->error($e->getMessage());
            }
            if ($result !== false) {
                return $this->success(__('Added successfully'));
            }
            return $this->error(__('No rows were added'));
        }

        return $this->error(__('Parameter error'));
    }

    public function edit(Request $request): Response
    {
        $response = $this->initializeBackend($request);
        if ($response !== null) return $response;

        $id  = $request->get($this->model->getPk()) ?? $request->post($this->model->getPk());
        $row = $this->model->find($id);
        if (!$row) {
            return $this->error(__('Record not found'));
        }

        $dataLimitAdminIds = $this->getDataLimitAdminIds();
        if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
            return $this->error(__('You have no permission'));
        }

        if ($request->method() === 'POST') {
            $data = $request->post();
            if (!$data) {
                return $this->error(__('Parameter %s can not be empty', ['']));
            }

            $data   = $this->excludeFields($data);
            $result = false;
            $this->model->startTrans();
            try {
                if ($this->modelValidate) {
                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    if (class_exists($validate)) {
                        $validate = new $validate();
                        if ($this->modelSceneValidate) {
                            $validate->scene('edit');
                        }
                        $validate->check($data);
                    }
                }
                if (isset($data['pid']) && $data['pid'] > 0) {
                    $parent = $this->model->where('id', $data['pid'])->find();
                    if ($parent && $parent['pid'] == $row['id']) {
                        $parent->pid = 0;
                        $parent->save();
                    }
                }
                $result = $row->save($data);
                $this->model->commit();
            } catch (Throwable $e) {
                $this->model->rollback();
                return $this->error($e->getMessage());
            }
            if ($result !== false) {
                return $this->success(__('Update successful'));
            }
            return $this->error(__('No rows updated'));
        }

        return $this->success('', [
            'row' => $row
        ]);
    }

    public function del(Request $request): Response
    {
        $response = $this->initializeBackend($request);
        if ($response !== null) return $response;

        $ids = $request->get('ids') ?? $request->post('ids') ?? [];
        $ids = is_array($ids) ? $ids : [];

        $subData = $this->model->where('pid', 'in', $ids)->column('pid', 'id');
        foreach ($subData as $key => $subDatum) {
            if (!in_array($key, $ids)) {
                return $this->error(__('Please delete the child element first, or use batch deletion'));
            }
        }

        return $this->delFromTrait($request);
    }

    public function select(Request $request): Response
    {
        $response = $this->initializeBackend($request);
        if ($response !== null) return $response;

        $data = $this->getMenus($request, [['type', 'in', ['menu_dir', 'menu']], ['status', '=', 1]]);

        if ($this->assembleTree) {
            $data = $this->tree->assembleTree($this->tree->getTreeArray($data, 'title'));
        }
        return $this->success('', [
            'options' => $data
        ]);
    }

    protected function getMenus(Request $request, array $where = []): array
    {
        $pk      = $this->model->getPk();
        $initKey = $request->get('initKey') ?? $pk;

        $ids = $this->auth->getRuleIds();

        if (!in_array('*', $ids)) {
            $where[] = ['id', 'in', $ids];
        }

        if ($this->keyword) {
            $keyword = explode(' ', $this->keyword);
            foreach ($keyword as $item) {
                $where[] = [$this->quickSearchField, 'like', '%' . $item . '%'];
            }
        }

        if ($this->initValue) {
            $where[] = [$initKey, 'in', $this->initValue];
        }

        $rules = $this->model
            ->where($where)
            ->order($this->queryOrderBuilder())
            ->select()
            ->toArray();

        return $this->assembleTree ? $this->tree->assembleChild($rules) : $rules;
    }

    private function autoAssignPermission(int $id, int $pid): void
    {
        $groups = AdminGroup::where('rules', '<>', '*')->select();
        foreach ($groups as $group) {
            /** @var AdminGroup $group */
            $rules = explode(',', (string) $group->rules);
            if (in_array($pid, $rules) && !in_array($id, $rules)) {
                $rules[]      = $id;
                $group->rules = implode(',', $rules);
                $group->save();
            }
        }
    }

    /**
     * 调用 trait 的 del 逻辑（因 Rule 重写了 del，需手动调用 trait）
     */
    private function delFromTrait(Request $request): Response
    {
        $where = [];
        $dataLimitAdminIds = $this->getDataLimitAdminIds();
        if ($dataLimitAdminIds) {
            $where[] = [$this->dataLimitField, 'in', $dataLimitAdminIds];
        }

        $ids = $request->get('ids') ?? $request->post('ids') ?? [];
        $ids = is_array($ids) ? $ids : [];
        $where[] = [$this->model->getPk(), 'in', $ids];
        $data = $this->model->where($where)->select();

        $count = 0;
        $this->model->startTrans();
        try {
            foreach ($data as $v) {
                $count += $v->delete();
            }
            $this->model->commit();
        } catch (Throwable $e) {
            $this->model->rollback();
            return $this->error($e->getMessage());
        }
        if ($count) {
            return $this->success(__('Deleted successfully'));
        }
        return $this->error(__('No rows were deleted'));
    }
}
