<?php

namespace Database\Seeders;

use App\Models\ImportEntity;
use Illuminate\Database\Seeder;

class ImportEntitySeeder extends Seeder
{
    public function run(): void
    {
        $defs = [
            [
                'key' => 'person',
                'canonical_root' => 'person',
                'label' => 'Person',
                'fields' => ['first_name', 'last_name', 'full_name', 'gender', 'birthday', 'tax_number', 'social_security_number', 'description', 'employer'],
                'field_aliases' => [
                    'dob' => 'birthday',
                    'date_of_birth' => 'birthday',
                    'name' => 'full_name',
                ],
                'aliases' => ['person'],
                'rules' => [
                    ['pattern' => '/^(ime|first\s*name|firstname)\b|\bime\b/i', 'field' => 'first_name'],
                    ['pattern' => '/^(priimek|last\s*name|lastname)\b|\bpriimek\b/i', 'field' => 'last_name'],
                    ['pattern' => '/^(naziv|ime\s+in\s+priimek|full\s*name|name)\b|\bnaziv\b/i', 'field' => 'full_name'],
                    ['pattern' => '/^(davcna|davčna|tax|tax\s*number|tin)\b/i', 'field' => 'tax_number'],
                    ['pattern' => '/^(emso|emšo|ssn|social|social\s*security)\b/i', 'field' => 'social_security_number'],
                    ['pattern' => '/^(spol|gender)\b/i', 'field' => 'gender'],
                    ['pattern' => '/^(rojstvo|datum\s*rojstva|dob|birth|birthday|date\s*of\s*birth)\b/i', 'field' => 'birthday'],
                    ['pattern' => '/^(komentar|opis|opomba|comment|description|note)\b/i', 'field' => 'description'],
                    ['pattern' => '/^(delodajalec|služba)\b/i', 'field' => 'employer']
                ],
                'ui' => ['order' => 1],
            ],
            [
                'key' => 'person_addresses',
                'canonical_root' => 'address',
                'label' => 'Person Addresses',
                'fields' => ['address', 'city', 'postal_code', 'country', 'type_id', 'description'],
                'supports_multiple' => true,
                'field_aliases' => [
                    'ulica' => 'address',
                    'naslov' => 'address',
                    'mesto' => 'city',
                    'posta' => 'postal_code',
                    'pošta' => 'postal_code',
                    'zip' => 'postal_code',
                    'drzava' => 'country',
                    'država' => 'country',
                    'opis' => 'description',
                ],
                'aliases' => ['person_addresses', 'address', 'addresses'],
                'rules' => [
                    ['pattern' => '/^(naslov|ulica|address)\b/i', 'field' => 'address'],
                    ['pattern' => '/^(mesto|city|kraj)\b/i', 'field' => 'city'],
                    ['pattern' => '/^(posta|pošta|zip|postal)\b/i', 'field' => 'postal_code'],
                    ['pattern' => '/^(drzava|država|country)\b/i', 'field' => 'country'],
                    ['pattern' => '/^(komentar|opis|opomba|comment|description|note)\b/i', 'field' => 'description'],
                ],
                'ui' => ['order' => 2],
            ],
            [
                'key' => 'person_phones',
                'canonical_root' => 'phone',
                'label' => 'Person Phones',
                'fields' => ['nu', 'country_code', 'type_id', 'description'],
                'supports_multiple' => true,
                'field_aliases' => ['number' => 'nu'],
                'aliases' => ['phone', 'person_phones'],
                'rules' => [
                    ['pattern' => '/^(telefon|tel\.?|gsm|mobile|phone|kontakt)\b/i', 'field' => 'nu'],
                ],
                'ui' => ['order' => 3],
            ],
            [
                'key' => 'emails',
                'canonical_root' => 'email',
                'label' => 'Emails',
                'fields' => ['value', 'is_primary', 'label'],
                'supports_multiple' => true,
                'field_aliases' => ['email' => 'value'],
                'aliases' => ['email', 'emails'],
                'rules' => [
                    ['pattern' => '/^(email|e-?mail|mail)\b/i', 'field' => 'value'],
                ],
                'ui' => ['order' => 4],
            ],
            [
                'key' => 'contracts',
                'canonical_root' => 'contract',
                'label' => 'Contracts',
                // Include 'meta' so the UI can select contract.meta as a field target
                'fields' => ['reference', 'start_date', 'end_date', 'description', 'type_id', 'client_case_id', 'meta'],
                'aliases' => ['contract', 'contracts', 'contracs'],
                'meta' => true,
                'rules' => [
                    ['pattern' => '/^(sklic|reference|ref)\b/i', 'field' => 'reference'],
                    ['pattern' => '/^(od|from|start|zacetek|začetek)\b/i', 'field' => 'start_date'],
                    ['pattern' => '/^(do|to|end|konec)\b/i', 'field' => 'end_date'],
                    ['pattern' => '/^(komentar|opis|opomba|comment|description|note)\b/i', 'field' => 'description'],
                ],
                'ui' => ['order' => 5],
            ],
            [
                'key' => 'accounts',
                'canonical_root' => 'account',
                'label' => 'Accounts',
                'fields' => ['reference', 'initial_amount', 'balance_amount', 'contract_id', 'contract_reference', 'type_id', 'active', 'description'],
                'aliases' => ['account', 'accounts'],
                'rules' => [
                    ['pattern' => '/^(dolg|znesek|amount|saldo|balance|debt)\b/i', 'field' => 'balance_amount'],
                    ['pattern' => '/^(sklic|reference|ref)\b/i', 'field' => 'reference'],
                ],
                'ui' => ['order' => 6],
            ],
            [
                'key' => 'client_cases',
                'canonical_root' => 'client_case',
                'label' => 'Client Cases',
                'fields' => ['client_ref'],
                'aliases' => ['client_case', 'client_cases', 'case', 'primeri', 'primer'],
                'rules' => [
                    ['pattern' => '/^(client\s*ref|client_ref|case\s*ref|case_ref|primer|primeri|zadeva)\b/i', 'field' => 'client_ref'],
                ],
                'ui' => ['order' => 7],
            ],
            [
                'key' => 'case_objects',
                'canonical_root' => 'case_object',
                'label' => 'Case Objects',
                'fields' => ['reference', 'name', 'description', 'type', 'contract_id'],
                'aliases' => ['case_object', 'case_objects', 'object', 'objects', 'predmet', 'predmeti'],
                'rules' => [
                    ['pattern' => '/^(sklic|reference|ref)\b/i', 'field' => 'reference'],
                    ['pattern' => '/^(ime|naziv|name|title)\b/i', 'field' => 'name'],
                    ['pattern' => '/^(tip|vrsta|type|kind)\b/i', 'field' => 'type'],
                    ['pattern' => '/^(komentar|opis|opomba|comment|description|note)\b/i', 'field' => 'description'],
                    ['pattern' => '/^(contract\s*id|contract_id|pogodba\s*id|pogodba_id)\b/i', 'field' => 'contract_id'],
                ],
                'ui' => ['order' => 8],
            ],
            [
                'key' => 'payments',
                'canonical_root' => 'payment',
                'label' => 'Payments',
                // include common fields and helpful references for mapping
                'fields' => [
                    'reference',
                    'payment_nu',
                    'payment_date',
                    'amount',
                    'type_id',
                    'active',
                    // optional helpers for mapping by related records
                    'debt_id',
                    'account_id',
                    'account_reference',
                    'contract_reference',
                ],
                'field_aliases' => [
                    'date' => 'payment_date',
                    'datum' => 'payment_date',
                    'paid_at' => 'payment_date',
                    'number' => 'payment_nu',
                    'znesek' => 'amount',
                    'value' => 'amount',
                ],
                'aliases' => ['payment', 'payments', 'placila', 'plačila'],
                'rules' => [
                    ['pattern' => '/^(sklic|reference|ref)\b/i', 'field' => 'reference'],
                    ['pattern' => '/^(stevilka|številka|number|payment\s*no\.?|payment\s*nu)\b/i', 'field' => 'payment_nu'],
                    ['pattern' => '/^(datum|date|paid\s*at|payment\s*date)\b/i', 'field' => 'payment_date'],
                    ['pattern' => '/^(znesek|amount|vplacilo|vplačilo|placilo|plačilo)\b/i', 'field' => 'amount'],
                ],
                'ui' => ['order' => 9],
            ],
            [
                'key' => 'activities',
                'canonical_root' => 'activity',
                'label' => 'Activities',
                'fields' => ['note', 'due_date', 'amount', 'action_id', 'decision_id', 'contract_id', 'client_case_id', 'user_id'],
                'field_aliases' => [
                    'opis' => 'note',
                    'datum' => 'due_date',
                    'rok' => 'due_date',
                    'znesek' => 'amount',
                ],
                'aliases' => ['activity', 'activities', 'opravilo', 'opravila'],
                'rules' => [
                    ['pattern' => '/^(aktivnost|activity|note|opis)\b/i', 'field' => 'note'],
                    ['pattern' => '/^(rok|due|datum|date)\b/i', 'field' => 'due_date'],
                    ['pattern' => '/^(znesek|amount|vrednost|value)\b/i', 'field' => 'amount'],
                    ['pattern' => '/^(akcija|action)\b/i', 'field' => 'action_id'],
                    ['pattern' => '/^(odlocitev|odločitev|decision)\b/i', 'field' => 'decision_id'],
                    ['pattern' => '/^(pogodba|contract)\b/i', 'field' => 'contract_id'],
                    ['pattern' => '/^(primer|case)\b/i', 'field' => 'client_case_id'],
                ],
                'ui' => ['order' => 10],
            ],
        ];

        foreach ($defs as $d) {
            ImportEntity::updateOrCreate(['key' => $d['key']], $d);
        }
    }
}
