<?php

declare(strict_types=1);

namespace PpPhp\Core\Designer\Templates;

use PpPhp\Core\Designer\DesignerComponent as C;
use PpPhp\Core\Designer\DesignerTemplate;

class ImportPageTemplate extends DesignerTemplate
{
    public function toArray(): array
    {
        return $this->basePage('Import ' . $this->title(), [
            C::breadcrumb('breadcrumb_main', [
                'Home',
                $this->title(),
                'Import',
            ]),

            C::heading(
                'page_heading',
                'Import ' . $this->title(),
                'Upload a file or paste copied data from Excel, CSV, or another source.'
            ),

            C::make('import_workflow_steps', 'workflow-steps', 'Import Steps', [
                'steps' => [
                    'Source',
                    'Mapping',
                    'Preview',
                    'Import',
                ],
            ]),

            C::make('import_source_section', 'section', 'Import Source', [
                'title' => 'Import Source',
                'collapsible' => true,
                'collapsed' => false,
            ], [
                C::make('import_source_tabs', 'tabs', 'Import Source Tabs', [
                    'tabs' => [
                        ['id' => 'paste_tab', 'label' => 'Copy / Paste Data'],
                        ['id' => 'upload_tab', 'label' => 'Upload File'],
                    ],
                    'active' => 'paste_tab',
                ]),

                C::make('paste_data_area', 'textarea', 'Paste Data Area', [
                    'label' => 'Paste Data',
                    'placeholder' => "Paste rows copied from Excel or CSV here...",
                    'rows' => 8,
                    'helperText' => 'Supports tab, comma, and semicolon separated data.',
                ]),

                C::make('paste_clipboard_btn', 'clipboard-button', 'Paste From Clipboard', [
                    'text' => 'Paste From Clipboard',
                    'target' => 'paste_data_area',
                    'icon' => 'clipboard',
                ]),

                C::make('upload_file_area', 'upload', 'Upload File', [
                    'label' => 'Upload File',
                    'accept' => '.csv,.xlsx,.json,.xml',
                    'maxSize' => '10MB',
                    'helperText' => 'Supported formats: CSV, XLSX, JSON, XML.',
                ]),

                C::make('download_sample_btn', 'sample-download-button', 'Download Sample CSV', [
                    'text' => 'Download Sample CSV',
                    'format' => 'csv',
                    'fields' => array_map(fn($field) => $field['name'] ?? '', $this->fields(1000)),
                    'icon' => 'download',
                ]),
            ]),

            C::make('import_options_section', 'section', 'Import Options', [
                'title' => 'Import Options',
                'collapsible' => true,
                'collapsed' => false,
            ], [
                C::make('import_options_row', 'row', 'Import Options Row', [
                    'columns' => 2,
                    'cells' => [
                        [
                            'id' => 'delimiter_cell',
                            'span' => 6,
                            'components' => [
                                C::make('delimiter_selector', 'select', 'Delimiter', [
                                    'label' => 'Delimiter',
                                    'options' => ['Auto Detect', 'Tab', 'Comma', 'Semicolon'],
                                    'defaultValue' => 'Auto Detect',
                                ]),
                            ],
                            'style' => C::style(),
                        ],
                        [
                            'id' => 'import_mode_cell',
                            'span' => 6,
                            'components' => [
                                C::make('import_mode_selector', 'select', 'Import Mode', [
                                    'label' => 'Import Mode',
                                    'options' => ['Insert only', 'Skip duplicates', 'Update existing', 'Insert + Update'],
                                    'defaultValue' => 'Insert only',
                                ]),
                            ],
                            'style' => C::style(),
                        ],
                    ],
                ]),

                C::make('first_row_headers', 'checkbox', 'First row contains headers', [
                    'label' => 'First row contains headers',
                    'checked' => true,
                ]),

                C::make('validate_before_import', 'checkbox', 'Validate before import', [
                    'label' => 'Validate before import',
                    'checked' => true,
                ]),
            ]),

            C::make('field_mapping_section', 'section', 'Field Mapping', [
                'title' => 'Field Mapping',
                'collapsible' => true,
                'collapsed' => true,
            ], [
                C::make('auto_map_fields', 'checkbox', 'Auto-map fields by name', [
                    'label' => 'Auto-map fields by name',
                    'checked' => true,
                ]),

                C::make('field_mapping_grid', 'field-mapper', 'Field Mapper', [
                    'sourceColumns' => ['Name', 'Email', 'Phone', 'Address',],
                    'targetFields' => array_map(
                        fn($f) => $f['name'] ?? '',
                        $this->fields(1000)
                    ),
                ]),
            ]),

            C::make('preview_section', 'section', 'Preview', [
                'title' => 'Preview',
                'collapsible' => true,
                'collapsed' => true,
            ], [
                C::make('import_preview_grid', 'table-grid', 'Import Preview Grid', [
                    'columns' => [['field' => 'name', 'label' => 'Name'], ['field' => 'email', 'label' => 'Email'], ['field' => 'phone', 'label' => 'Phone'],],
                    'sampleRows' => [['name' => 'John Smith', 'email' => 'john@example.com', 'phone' => '9801234567',], ['name' => 'Jane Doe', 'email' => 'jane@example.com', 'phone' => '9801234568',],],

                    'fields' => array_slice(
                        array_map(fn($field) => $field['name'] ?? '', $this->fields(1000)),
                        0,
                        6
                    ),
                    'selection' => false,
                    'showActions' => false,
                    'showSearch' => false,
                    'showAddNew' => false,
                    'bulkActions' => [],
                    'showTotals' => false,
                    'showTotalsText' => true,
                    'showPageSize' => false,
                    'density' => 'compact',
                ]),
            ]),

            C::make('import_summary_section', 'section', 'Import Summary', [
                'title' => 'Import Summary',
                'collapsible' => true,
                'collapsed' => true,
            ], [
                C::make('import_summary', 'import-summary', 'Import Summary', [
                    'rowsFound' => 250,
                    'mappedFields' => count(array_filter(array_map(fn($field) => $field['name'] ?? '', $this->fields(1000)))),
                    'warnings' => 1,
                    'errors' => 0,
                ]),
            ]),

            C::make('import_actions_row', 'row', 'Import Actions Row', [
                'columns' => 1,
                'cells' => [
                    [
                        'id' => 'import_actions_cell',
                        'span' => 12,
                        'components' => [
                            C::buttonGroup('import_actions', [
                                C::button('preview_import_btn', 'Validate & Preview', 'secondary'),
                                C::button('run_import_btn', 'Import', 'primary'),
                                C::button('cancel_import_btn', 'Cancel', 'light'),
                            ], 'Import Actions'),
                        ],
                        'style' => C::style([
                            'display' => 'flex',
                            'justifyContent' => 'flex-end',
                            'gap' => '8px',
                        ]),
                    ],
                ],
            ]),
        ]);
    }
}
