<?php
declare(strict_types=1);

namespace App\Controllers;

require_once BASE_PATH . '/app/helpers/permissions.php';

use App\Core\Controller;
use App\Models\CustomFieldModel;

final class CustomFieldController extends Controller
{
    public function index(): void
    {
        require_permission('custom_fields.view');

        $module = trim((string) ($_GET['module'] ?? ''));

        $model = new CustomFieldModel();

        $this->view('admin/custom_fields/index', [
            'rows' => $module !== '' ? $model->all($module) : $model->all(),
            'selectedModule' => $module,
        ]);
    }

    public function create(): void
    {
        require_permission('custom_fields.manage');

        $model = new CustomFieldModel();

        if ($this->isPost()) {
            csrf_check();

            $data = $this->postedData();

            if ($data['module_name'] === '' || $data['field_key'] === '' || $data['label_en'] === '' || $data['label_ar'] === '') {
                flash('danger', trans('required_fields'));
                redirect('admin/custom-fields/create');
            }

            $id = $model->create($data);
            audit_log('create', 'custom_fields', $id, $data);
            flash('success', trans('custom_field_saved_success'));
            redirect('admin/custom-fields');
        }

        $this->view('admin/custom_fields/create');
    }

    public function edit(): void
    {
        require_permission('custom_fields.manage');

        $id = (int) ($_GET['id'] ?? 0);
        $model = new CustomFieldModel();
        $row = $model->findById($id);

        if (!$row) {
            redirect('admin/custom-fields');
        }

        if ($this->isPost()) {
            csrf_check();

            $data = $this->postedData();

            if ($data['module_name'] === '' || $data['field_key'] === '' || $data['label_en'] === '' || $data['label_ar'] === '') {
                flash('danger', trans('required_fields'));
                redirect('admin/custom-fields/edit?id=' . $id);
            }

            $model->update($id, $data);
            audit_log('update', 'custom_fields', $id, $data);
            flash('success', trans('custom_field_saved_success'));
            redirect('admin/custom-fields');
        }

        $this->view('admin/custom_fields/edit', [
            'row' => $row,
        ]);
    }

    private function postedData(): array
    {
        return [
            'module_name' => trim((string) ($_POST['module_name'] ?? '')),
            'field_key' => trim((string) ($_POST['field_key'] ?? '')),
            'label_en' => trim((string) ($_POST['label_en'] ?? '')),
            'label_ar' => trim((string) ($_POST['label_ar'] ?? '')),
            'field_type' => trim((string) ($_POST['field_type'] ?? 'text')),
            'field_options' => trim((string) ($_POST['field_options'] ?? '')),
            'placeholder_en' => trim((string) ($_POST['placeholder_en'] ?? '')),
            'placeholder_ar' => trim((string) ($_POST['placeholder_ar'] ?? '')),
            'help_text_en' => trim((string) ($_POST['help_text_en'] ?? '')),
            'help_text_ar' => trim((string) ($_POST['help_text_ar'] ?? '')),
            'is_required' => isset($_POST['is_required']) ? 1 : 0,
            'is_active' => isset($_POST['is_active']) ? 1 : 0,
            'sort_order' => (int) ($_POST['sort_order'] ?? 0),
        ];
    }
}
