<?php

namespace Modules\SystemCore\App\Services;

use App\Services\Settings\BaseModuleSettings;

class SystemCoreSettings extends BaseModuleSettings
{
    protected string $module = 'SystemCore';

    /**
     * Define module settings
     *
     * These settings configure:
     * - Code prefixes for customers, suppliers, products, and orders
     * - Numbering patterns (padding, date inclusion)
     * - Posting log settings for module-to-module synchronization
     */
    protected function define(): array
    {
        return [
            'prefixes' => [
                'customer_prefix' => [
                    'label' => __('Customer Code Prefix'),
                    'type' => 'text',
                    'default' => 'CUST',
                    'validation' => 'required|string|max:10',
                    'attributes' => ['maxlength' => '10'],
                    'help' => __('Prefix for customer codes (e.g., CUST-000001)'),
                ],
                'supplier_prefix' => [
                    'label' => __('Supplier Code Prefix'),
                    'type' => 'text',
                    'default' => 'SUPP',
                    'validation' => 'required|string|max:10',
                    'attributes' => ['maxlength' => '10'],
                    'help' => __('Prefix for supplier codes (e.g., SUPP-000001)'),
                ],
                'product_prefix' => [
                    'label' => __('Product Code Prefix'),
                    'type' => 'text',
                    'default' => 'PROD',
                    'validation' => 'required|string|max:10',
                    'attributes' => ['maxlength' => '10'],
                    'help' => __('Prefix for product codes (e.g., PROD-000001)'),
                ],
                'sales_order_prefix' => [
                    'label' => __('Sales Order Prefix'),
                    'type' => 'text',
                    'default' => 'SO',
                    'validation' => 'required|string|max:10',
                    'attributes' => ['maxlength' => '10'],
                    'help' => __('Prefix for sales order numbers (e.g., SO-20241114-00001)'),
                ],
                'purchase_order_prefix' => [
                    'label' => __('Purchase Order Prefix'),
                    'type' => 'text',
                    'default' => 'PO',
                    'validation' => 'required|string|max:10',
                    'attributes' => ['maxlength' => '10'],
                    'help' => __('Prefix for purchase order numbers (e.g., PO-20241114-00001)'),
                ],
            ],
            'numbering' => [
                'customer_code_padding' => [
                    'label' => __('Customer Code Padding'),
                    'type' => 'number',
                    'default' => 6,
                    'validation' => 'required|integer|min:4|max:10',
                    'attributes' => ['min' => 4, 'max' => 10],
                    'help' => __('Number of digits for customer code (e.g., 6 for 000001)'),
                ],
                'supplier_code_padding' => [
                    'label' => __('Supplier Code Padding'),
                    'type' => 'number',
                    'default' => 6,
                    'validation' => 'required|integer|min:4|max:10',
                    'attributes' => ['min' => 4, 'max' => 10],
                    'help' => __('Number of digits for supplier code (e.g., 6 for 000001)'),
                ],
                'product_code_padding' => [
                    'label' => __('Product Code Padding'),
                    'type' => 'number',
                    'default' => 6,
                    'validation' => 'required|integer|min:4|max:10',
                    'attributes' => ['min' => 4, 'max' => 10],
                    'help' => __('Number of digits for product code (e.g., 6 for 000001)'),
                ],
                'order_number_padding' => [
                    'label' => __('Order Number Padding'),
                    'type' => 'number',
                    'default' => 5,
                    'validation' => 'required|integer|min:3|max:8',
                    'attributes' => ['min' => 3, 'max' => 8],
                    'help' => __('Number of digits for order sequence (e.g., 5 for 00001)'),
                ],
                'include_date_in_order_number' => [
                    'label' => __('Include Date in Order Numbers'),
                    'type' => 'toggle',
                    'default' => true,
                    'validation' => 'boolean',
                    'help' => __('Include date in order numbers (e.g., SO-20241114-00001)'),
                ],
            ],
            'posting' => [
                'enable_posting_log' => [
                    'label' => __('Enable Posting Log'),
                    'type' => 'toggle',
                    'default' => true,
                    'validation' => 'boolean',
                    'help' => __('Log all module-to-module data synchronization operations for auditing and troubleshooting'),
                ],
                'posting_log_retention_days' => [
                    'label' => __('Posting Log Retention (Days)'),
                    'type' => 'number',
                    'default' => 0,
                    'validation' => 'required|integer|min:0|max:3650',
                    'attributes' => ['min' => 0, 'max' => 3650],
                    'help' => __('Number of days to keep posting log entries. Set to 0 to keep forever (no auto-purge).'),
                ],
                'max_retry_attempts' => [
                    'label' => __('Max Retry Attempts'),
                    'type' => 'number',
                    'default' => 3,
                    'validation' => 'required|integer|min:1|max:10',
                    'attributes' => ['min' => 1, 'max' => 10],
                    'help' => __('Maximum number of retry attempts for failed postings'),
                ],
            ],
        ];
    }

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

    /**
     * Get module description
     */
    public function getModuleDescription(): string
    {
        return __('Configure code prefixes, numbering patterns, and synchronization settings');
    }

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

    /**
     * Convert settings format to fields format for view
     */
    private function convertFieldsFormat(array $settings): array
    {
        $fields = [];
        foreach ($settings as $name => $field) {
            $fieldData = [
                'name' => $name,
                'label' => $field['label'],
                'type' => $field['type'],
                'default' => $field['default'],
                'help' => $field['help'] ?? null,
            ];

            // Check if field is required
            $validationRules = explode('|', $field['validation'] ?? '');
            $fieldData['required'] = in_array('required', $validationRules);

            // Add type-specific attributes
            if ($field['type'] === 'number' && isset($field['attributes'])) {
                $fieldData['min'] = $field['attributes']['min'] ?? null;
                $fieldData['max'] = $field['attributes']['max'] ?? null;
            }

            if ($field['type'] === 'text' && isset($field['attributes'])) {
                $fieldData['maxlength'] = $field['attributes']['maxlength'] ?? null;
            }

            $fields[] = $fieldData;
        }

        return $fields;
    }

    /**
     * Get settings sections
     */
    public function getSections(): array
    {
        $settings = $this->define();

        return [
            [
                'id' => 'prefixes',
                'title' => __('Code Prefixes'),
                'icon' => 'bx bx-hash',
                'description' => __('Configure prefixes for customer, supplier, product, and order codes'),
                'fields' => $this->convertFieldsFormat($settings['prefixes']),
            ],
            [
                'id' => 'numbering',
                'title' => __('Numbering Patterns'),
                'icon' => 'bx bx-sort-alt-2',
                'description' => __('Configure automatic numbering patterns for codes and orders'),
                'fields' => $this->convertFieldsFormat($settings['numbering']),
            ],
            [
                'id' => 'posting',
                'title' => __('Posting Log Settings'),
                'icon' => 'bx bx-history',
                'description' => __('Configure posting log settings for tracking module-to-module data synchronization'),
                'fields' => $this->convertFieldsFormat($settings['posting']),
            ],
        ];
    }

    /**
     * Get validation rules
     */
    public function getValidationRules(): array
    {
        return [
            // Prefixes
            'customer_prefix' => 'required|string|max:10',
            'supplier_prefix' => 'required|string|max:10',
            'product_prefix' => 'required|string|max:10',
            'sales_order_prefix' => 'required|string|max:10',
            'purchase_order_prefix' => 'required|string|max:10',
            // Numbering
            'customer_code_padding' => 'required|integer|min:4|max:10',
            'supplier_code_padding' => 'required|integer|min:4|max:10',
            'product_code_padding' => 'required|integer|min:4|max:10',
            'order_number_padding' => 'required|integer|min:3|max:8',
            'include_date_in_order_number' => 'boolean',
            // Posting
            'enable_posting_log' => 'boolean',
            'posting_log_retention_days' => 'required|integer|min:0|max:3650',
            'max_retry_attempts' => 'required|integer|min:1|max:10',
        ];
    }

    /**
     * Get default values
     */
    public function getDefaults(): array
    {
        return [
            // Prefixes
            'customer_prefix' => 'CUST',
            'supplier_prefix' => 'SUPP',
            'product_prefix' => 'PROD',
            'sales_order_prefix' => 'SO',
            'purchase_order_prefix' => 'PO',
            // Numbering
            'customer_code_padding' => 6,
            'supplier_code_padding' => 6,
            'product_code_padding' => 6,
            'order_number_padding' => 5,
            'include_date_in_order_number' => true,
            // Posting
            'enable_posting_log' => true,
            'posting_log_retention_days' => 0,
            'max_retry_attempts' => 3,
        ];
    }
}
