<?php

namespace Modules\PMCore\App\Settings;

use App\Services\Settings\BaseModuleSettings;

class PMCoreSettings extends BaseModuleSettings
{
    protected string $module = 'PMCore';

    /**
     * Get module display name
     */
    public function getModuleName(): string
    {
        return __('Project Management Settings');
    }

    /**
     * Get module description
     */
    public function getModuleDescription(): string
    {
        return __('Configure project defaults and code generation settings');
    }

    /**
     * Get module icon
     */
    public function getModuleIcon(): string
    {
        return 'bx bx-briefcase';
    }

    /**
     * Define module settings
     *
     * These settings are actively used:
     * - ProjectController::store() uses default_project_status, default_project_priority, default_is_billable
     * - Project::generateProjectCode() uses auto_generate_codes, code_prefix_length, code_separator
     */
    protected function define(): array
    {
        return [
            'project_defaults' => [
                'default_project_status' => [
                    'type' => 'select',
                    'label' => __('Default Project Status'),
                    'help' => __('Default status for new projects'),
                    'default' => 'planning',
                    'options' => [
                        'planning' => __('Planning'),
                        'in_progress' => __('In Progress'),
                        'on_hold' => __('On Hold'),
                        'completed' => __('Completed'),
                        'cancelled' => __('Cancelled'),
                    ],
                    'validation' => 'required|string',
                ],
                'default_project_priority' => [
                    'type' => 'select',
                    'label' => __('Default Project Priority'),
                    'help' => __('Default priority for new projects'),
                    'default' => 'medium',
                    'options' => [
                        'low' => __('Low'),
                        'medium' => __('Medium'),
                        'high' => __('High'),
                        'urgent' => __('Urgent'),
                    ],
                    'validation' => 'required|string',
                ],
                'default_is_billable' => [
                    'type' => 'toggle',
                    'label' => __('Projects Billable by Default'),
                    'help' => __('New projects are billable by default'),
                    'default' => true,
                    'validation' => 'boolean',
                ],
            ],

            'project_code' => [
                'auto_generate_codes' => [
                    'type' => 'toggle',
                    'label' => __('Auto-Generate Project Codes'),
                    'help' => __('Automatically generate unique codes for projects'),
                    'default' => true,
                    'validation' => 'boolean',
                ],
                'code_prefix_length' => [
                    'type' => 'number',
                    'label' => __('Code Prefix Length'),
                    'help' => __('Number of characters from project name (1-10)'),
                    'default' => '3',
                    'validation' => 'required|numeric|min:1|max:10',
                ],
                'code_separator' => [
                    'type' => 'text',
                    'label' => __('Code Separator'),
                    'help' => __('Character between prefix and number'),
                    'default' => '-',
                    'validation' => 'nullable|string|max:1',
                ],
            ],
        ];
    }

    /**
     * Get default values for all settings
     */
    public function getDefaults(): array
    {
        return [
            // Project Defaults
            'default_project_status' => 'planning',
            'default_project_priority' => 'medium',
            'default_is_billable' => true,
            // Project Code
            'auto_generate_codes' => true,
            'code_prefix_length' => '3',
            'code_separator' => '-',
        ];
    }

    /**
     * Get settings sections for the view
     */
    public function getSections(): array
    {
        return [
            [
                'id' => 'project_defaults',
                'title' => __('Project Defaults'),
                'icon' => 'bx bx-folder',
                'description' => __('Configure default values for new projects'),
                'fields' => [
                    [
                        'name' => 'default_project_status',
                        'label' => __('Default Project Status'),
                        'type' => 'select',
                        'default' => 'planning',
                        'required' => true,
                        'options' => [
                            'planning' => __('Planning'),
                            'in_progress' => __('In Progress'),
                            'on_hold' => __('On Hold'),
                            'completed' => __('Completed'),
                            'cancelled' => __('Cancelled'),
                        ],
                        'help' => __('Default status for new projects'),
                    ],
                    [
                        'name' => 'default_project_priority',
                        'label' => __('Default Project Priority'),
                        'type' => 'select',
                        'default' => 'medium',
                        'required' => true,
                        'options' => [
                            'low' => __('Low'),
                            'medium' => __('Medium'),
                            'high' => __('High'),
                            'urgent' => __('Urgent'),
                        ],
                        'help' => __('Default priority for new projects'),
                    ],
                    [
                        'name' => 'default_is_billable',
                        'label' => __('Projects Billable by Default'),
                        'type' => 'switch',
                        'default' => true,
                        'help' => __('New projects are billable by default'),
                    ],
                ],
            ],
            [
                'id' => 'project_code',
                'title' => __('Project Code Generation'),
                'icon' => 'bx bx-hash',
                'description' => __('Configure how project codes are generated'),
                'fields' => [
                    [
                        'name' => 'auto_generate_codes',
                        'label' => __('Auto-Generate Project Codes'),
                        'type' => 'switch',
                        'default' => true,
                        'help' => __('Automatically generate unique codes for projects'),
                    ],
                    [
                        'name' => 'code_prefix_length',
                        'label' => __('Code Prefix Length'),
                        'type' => 'number',
                        'default' => '3',
                        'min' => '1',
                        'max' => '10',
                        'required' => true,
                        'help' => __('Number of characters from project name (1-10)'),
                    ],
                    [
                        'name' => 'code_separator',
                        'label' => __('Code Separator'),
                        'type' => 'text',
                        'default' => '-',
                        'required' => false,
                        'help' => __('Character between prefix and number'),
                    ],
                ],
            ],
        ];
    }

    /**
     * Get validation rules
     */
    public function getValidationRules(): array
    {
        return [
            'default_project_status' => 'required|string',
            'default_project_priority' => 'required|string',
            'default_is_billable' => 'boolean',
            'auto_generate_codes' => 'boolean',
            'code_prefix_length' => 'required|numeric|min:1|max:10',
            'code_separator' => 'nullable|string|max:1',
        ];
    }
}
