<?php

namespace App\Modules\Citizenship\Custom;

use App\Core\Citizenship\AbstractCitizenship;

class Custom extends AbstractCitizenship
{
    public function getIndividualIdLabel(): string
    {
        return $this->getConfig('individual_label', 'ID Number');
    }

    public function getCompanyIdLabel(): string
    {
        return $this->getConfig('company_label', 'Tax ID');
    }

    public function getIndividualIdPlaceholder(): string
    {
        return $this->getConfig('individual_placeholder', '');
    }

    public function getCompanyIdPlaceholder(): string
    {
        return $this->getConfig('company_placeholder', '');
    }

    public function getVerificationFields(): array
    {
        $fields = [];

        // Build fields from config — admin can define custom fields
        $customFields = $this->getConfig('custom_fields', '');

        if (! empty($customFields)) {
            // Parse "key:label:group" format, one per line
            $lines = array_filter(array_map('trim', explode("\n", $customFields)));
            foreach ($lines as $line) {
                $parts = array_map('trim', explode(':', $line));
                $key = $parts[0] ?? '';
                $label = $parts[1] ?? $key;
                $group = $parts[2] ?? 'general';

                if (! empty($key)) {
                    $fields[$key] = [
                        'type' => 'text',
                        'label' => $label,
                        'placeholder' => '',
                        'group' => $group,
                    ];
                }
            }
        }

        // Default fields if no custom fields defined
        if (empty($fields)) {
            $fields = [
                'individual_id' => ['type' => 'text', 'label' => $this->getConfig('individual_label', 'ID Number'), 'placeholder' => $this->getConfig('individual_placeholder', ''), 'group' => 'individual'],
                'company_id' => ['type' => 'text', 'label' => $this->getConfig('company_label', 'Tax ID'), 'placeholder' => $this->getConfig('company_placeholder', ''), 'group' => 'company'],
            ];
        }

        return $fields;
    }

    public function validateIndividualId(string $idNumber, array $params = []): array
    {
        $minLength = (int) $this->getConfig('min_length', 3);
        $maxLength = (int) $this->getConfig('max_length', 30);

        $idNumber = trim($idNumber);

        if (strlen($idNumber) < $minLength || strlen($idNumber) > $maxLength) {
            return $this->failResult("ID must be between {$minLength} and {$maxLength} characters.");
        }

        return $this->successResult();
    }

    public function validateCompanyId(string $companyNumber, array $params = []): array
    {
        return $this->validateIndividualId($companyNumber, $params);
    }

    public function validateField(string $fieldKey, string $value, array $allData = []): array
    {
        // Custom module: basic length validation for all fields
        $value = trim($value);
        if (strlen($value) < 2) {
            return $this->failResult('Bu alan en az 2 karakter olmalıdır.');
        }

        return $this->successResult();
    }

    public function getConfigFields(): array
    {
        return [
            'individual_label' => ['type' => 'text', 'label' => 'Bireysel ID Label', 'default' => 'ID Number'],
            'individual_placeholder' => ['type' => 'text', 'label' => 'Bireysel ID Placeholder', 'default' => ''],
            'company_label' => ['type' => 'text', 'label' => 'Kurumsal ID Label', 'default' => 'Tax ID'],
            'company_placeholder' => ['type' => 'text', 'label' => 'Kurumsal ID Placeholder', 'default' => ''],
            'min_length' => ['type' => 'number', 'label' => 'Minimum Karakter', 'default' => 3],
            'max_length' => ['type' => 'number', 'label' => 'Maksimum Karakter', 'default' => 30],
            'custom_fields' => [
                'type' => 'textarea',
                'label' => 'Özel Alanlar',
                'default' => '',
                'description' => 'Her satıra bir alan: key:Label:group (group: individual, company, general)',
            ],
        ];
    }

    protected function getModuleLabel(): string
    {
        return 'XX — Custom Configurable Module';
    }

    protected function getModuleDescription(): string
    {
        return 'Configurable verification module with admin-defined fields';
    }
}
