<?php

namespace Database\Seeders;

use App\Models\ImportTemplate;
use App\Models\ImportTemplateMapping;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class ImportTemplateSeeder extends Seeder
{
    public function run(): void
    {
        $template = ImportTemplate::query()->firstOrCreate([
            'name' => 'Person basic CSV',
        ], [
            'uuid' => (string) Str::uuid(),
            'description' => 'Basic person import: name, email, phone, address',
            'source_type' => 'csv',
            'default_record_type' => 'person',
            'sample_headers' => ['first_name', 'last_name', 'email', 'phone', 'address', 'city', 'postal_code', 'country'],
            'is_active' => true,
            'meta' => [
                'delimiter' => ',',
                'enclosure' => '"',
                'escape' => '\\',
            ],
        ]);

        $mappings = [
            ['source_column' => 'first_name', 'target_field' => 'person.first_name', 'position' => 1],
            ['source_column' => 'last_name', 'target_field' => 'person.last_name', 'position' => 2],
            ['source_column' => 'email', 'target_field' => 'person.email', 'position' => 3],
            ['source_column' => 'phone', 'target_field' => 'person.phone', 'position' => 4],
            ['source_column' => 'address', 'target_field' => 'person.address.street', 'position' => 5],
            ['source_column' => 'city', 'target_field' => 'person.address.city', 'position' => 6],
            ['source_column' => 'postal_code', 'target_field' => 'person.address.postal_code', 'position' => 7],
            ['source_column' => 'country', 'target_field' => 'person.address.country', 'position' => 8],
        ];

        foreach ($mappings as $map) {
            ImportTemplateMapping::firstOrCreate([
                'import_template_id' => $template->id,
                'source_column' => $map['source_column'],
            ], [
                'target_field' => $map['target_field'],
                'position' => $map['position'],
            ]);
        }

        // Multi-contacts template demonstrating group support for emails/phones/addresses
        $multi = ImportTemplate::query()->firstOrCreate([
            'name' => 'Person CSV (multi contacts)',
        ], [
            'uuid' => (string) Str::uuid(),
            'description' => 'Person import with multiple emails/phones/addresses per row using group 1 and 2',
            'source_type' => 'csv',
            'default_record_type' => 'person',
            'sample_headers' => [
                'first_name', 'last_name', 'Email 1', 'Email 2', 'Phone 1', 'Phone 2',
                'Address 1', 'City 1', 'Postal 1', 'Country 1',
                'Address 2', 'City 2', 'Postal 2', 'Country 2',
            ],
            'is_active' => true,
            'meta' => [
                'delimiter' => ',',
                'enclosure' => '"',
                'escape' => '\\',
            ],
        ]);

        $multiMappings = [
            // Person identity
            ['source_column' => 'first_name', 'target_field' => 'person.first_name', 'position' => 1],
            ['source_column' => 'last_name', 'target_field' => 'person.last_name', 'position' => 2],

            // Emails (groups 1, 2)
            ['source_column' => 'Email 1', 'target_field' => 'email.value', 'position' => 3, 'options' => ['group' => '1']],
            ['source_column' => 'Email 2', 'target_field' => 'email.value', 'position' => 4, 'options' => ['group' => '2']],

            // Phones (groups 1, 2)
            ['source_column' => 'Phone 1', 'target_field' => 'phone.nu', 'position' => 5, 'options' => ['group' => '1']],
            ['source_column' => 'Phone 2', 'target_field' => 'phone.nu', 'position' => 6, 'options' => ['group' => '2']],

            // Address group 1
            ['source_column' => 'Address 1', 'target_field' => 'address.address', 'position' => 7, 'options' => ['group' => '1']],
            ['source_column' => 'City 1', 'target_field' => 'address.city', 'position' => 8, 'options' => ['group' => '1']],
            ['source_column' => 'Postal 1', 'target_field' => 'address.postal_code', 'position' => 9, 'options' => ['group' => '1']],
            ['source_column' => 'Country 1', 'target_field' => 'address.country', 'position' => 10, 'options' => ['group' => '1']],

            // Address group 2
            ['source_column' => 'Address 2', 'target_field' => 'address.address', 'position' => 11, 'options' => ['group' => '2']],
            ['source_column' => 'City 2', 'target_field' => 'address.city', 'position' => 12, 'options' => ['group' => '2']],
            ['source_column' => 'Postal 2', 'target_field' => 'address.postal_code', 'position' => 13, 'options' => ['group' => '2']],
            ['source_column' => 'Country 2', 'target_field' => 'address.country', 'position' => 14, 'options' => ['group' => '2']],
        ];

        foreach ($multiMappings as $map) {
            ImportTemplateMapping::firstOrCreate([
                'import_template_id' => $multi->id,
                'source_column' => $map['source_column'],
            ], [
                'target_field' => $map['target_field'],
                'position' => $map['position'],
                'options' => $map['options'] ?? null,
            ]);
        }

        // Contracts with Meta (groups + keys) demo
        $contractsMeta = ImportTemplate::query()->firstOrCreate([
            'name' => 'Contracts CSV (meta groups demo)',
        ], [
            'uuid' => (string) Str::uuid(),
            'description' => 'Contracts import demonstrating contract.meta mappings using groups and keys.',
            'source_type' => 'csv',
            'default_record_type' => 'contract',
            'sample_headers' => [
                'Reference', 'Start', 'End',
                'Note', 'Category', 'Custom ID',
                'Legal Note', 'Legal ID',
            ],
            'is_active' => true,
            'meta' => [
                'delimiter' => ',',
                'enclosure' => '"',
                'escape' => '\\',
            ],
        ]);

        $contractsMetaMappings = [
            // Core contract fields
            ['source_column' => 'Reference', 'target_field' => 'contract.reference', 'position' => 1],
            ['source_column' => 'Start', 'target_field' => 'contract.start_date', 'position' => 2],
            ['source_column' => 'End', 'target_field' => 'contract.end_date', 'position' => 3],

            // Meta group 1 using explicit options.key
            ['source_column' => 'Note', 'target_field' => 'contract.meta', 'position' => 4, 'options' => ['group' => '1', 'key' => 'note']],
            ['source_column' => 'Category', 'target_field' => 'contract.meta', 'position' => 5, 'options' => ['group' => '1', 'key' => 'category']],

            // Meta group 1 using bracket key syntax (no options.key needed)
            ['source_column' => 'Custom ID', 'target_field' => 'contract.meta[custom_id]', 'position' => 6, 'options' => ['group' => '1']],

            // Meta group 2 examples
            ['source_column' => 'Legal Note', 'target_field' => 'contract.meta', 'position' => 7, 'options' => ['group' => '2', 'key' => 'note']],
            ['source_column' => 'Legal ID', 'target_field' => 'contract.meta[legal_id]', 'position' => 8, 'options' => ['group' => '2']],
        ];

        foreach ($contractsMetaMappings as $map) {
            ImportTemplateMapping::firstOrCreate([
                'import_template_id' => $contractsMeta->id,
                'source_column' => $map['source_column'],
            ], [
                'target_field' => $map['target_field'],
                'position' => $map['position'],
                'options' => $map['options'] ?? null,
            ]);
        }

        // Activities linked to contracts demo
        $activities = ImportTemplate::query()->firstOrCreate([
            'name' => 'Activities CSV (contract linked)',
        ], [
            'uuid' => (string) Str::uuid(),
            'description' => 'Activities import linked to existing contracts via reference.',
            'source_type' => 'csv',
            'default_record_type' => 'activity',
            'sample_headers' => ['contract_reference', 'note', 'due_date', 'amount', 'action', 'decision', 'user_email'],
            'is_active' => true,
            'meta' => [
                'delimiter' => ',',
                'enclosure' => '"',
                'escape' => '\\',
            ],
        ]);

        $activityMappings = [
            ['source_column' => 'contract_reference', 'target_field' => 'contract.reference', 'position' => 1],
            ['source_column' => 'note', 'target_field' => 'activity.note', 'position' => 2],
            ['source_column' => 'due_date', 'target_field' => 'activity.due_date', 'position' => 3],
            ['source_column' => 'amount', 'target_field' => 'activity.amount', 'position' => 4],
            ['source_column' => 'action', 'target_field' => 'activity.action_id', 'position' => 5],
            ['source_column' => 'decision', 'target_field' => 'activity.decision_id', 'position' => 6],
        ];

        foreach ($activityMappings as $map) {
            ImportTemplateMapping::firstOrCreate([
                'import_template_id' => $activities->id,
                'source_column' => $map['source_column'],
            ], [
                'target_field' => $map['target_field'],
                'position' => $map['position'],
                'options' => $map['options'] ?? null,
            ]);
        }
    }
}
