<?php

declare(strict_types=1);

namespace PpPhp\Core\Designer\Templates;

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

class ExportPageTemplate extends DesignerTemplate
{
    public function toArray(): array
    {
        $fields = $this->fields(1000);
        $fieldNames = array_map(fn($field) => $field['name'] ?? '', $fields);

        return $this->basePage('Export ' . $this->title(), [
            C::breadcrumb('breadcrumb_main', [
                'Home',
                $this->title(),
                'Export',
            ]),

            C::heading(
                'page_heading',
                'Export ' . $this->title(),
                'Choose records, fields, and format before exporting data.'
            ),

            C::make('export_workflow_steps', 'workflow-steps', 'Export Steps', [
                'steps' => [
                    'Records',
                    'Fields',
                    'Format',
                    'Export',
                ],
            ]),

            C::make('export_source_section', 'section', 'Export Source', [
                'title' => 'Export Source',
                'collapsible' => true,
                'collapsed' => false,
            ], [
                C::make('export_scope_selector', 'radio-group', 'Export Scope', [
                    'label' => 'Export Scope',
                    'options' => [
                        'Current Page',
                        'Selected Records',
                        'Filtered Records',
                        'All Records',
                    ],
                    'defaultValue' => 'Filtered Records',
                ]),
            ]),

            C::make('export_fields_section', 'section', 'Export Fields', [
                'title' => 'Export Fields',
                'collapsible' => true,
                'collapsed' => false,
            ], [
                C::make('export_field_selector', 'field-selector', 'Field Selector', [
                    'fields' => $fieldNames,
                ]),
            ]),

            C::make('export_format_section', 'section', 'Export Format', [
                'title' => 'Export Format',
                'collapsible' => true,
                'collapsed' => false,
            ], [
                C::make('export_format_selector', 'export-format-selector', 'Export Format', [
                    'formats' => ['CSV', 'XLSX', 'PDF', 'JSON', 'XML', 'HTML'],
                    'defaultValue' => 'XLSX',
                ]),
            ]),

            C::make('export_preview_section', 'section', 'Export Preview', [
                'title' => 'Export Preview',
                'collapsible' => true,
                'collapsed' => false,
            ], [
                C::make('export_preview_grid', 'table-grid', 'Export Preview Grid', [
                    'columns' => array_map(
                        fn($name) => [
                            'field' => $name,
                            'label' => ucwords(str_replace('_', ' ', $name)),
                        ],
                        array_slice($fieldNames, 0, 6)
                    ),
                    'sampleRows' => [
                        [
                            'id' => '1001',
                            'first_name' => 'John',
                            'last_name' => 'Smith',
                            'email' => 'john@example.com',
                            'dob_ad' => '2026-01-01',
                            'dob_bs' => '2082-09-17',
                        ],
                        [
                            'id' => '1002',
                            'first_name' => 'Jane',
                            'last_name' => 'Doe',
                            'email' => 'jane@example.com',
                            'dob_ad' => '2026-01-02',
                            'dob_bs' => '2082-09-18',
                        ],
                    ],
                    'selection' => false,
                    'showActions' => false,
                    'showSearch' => false,
                    'showAddNew' => false,
                    'bulkActions' => [],
                    'showTotals' => false,
                    'showTotalsText' => true,
                    'showPageSize' => false,
                    'density' => 'compact',
                ]),
            ]),

            C::make('export_summary_section', 'section', 'Export Summary', [
                'title' => 'Export Summary',
                'collapsible' => true,
                'collapsed' => false,
            ], [
                C::make('export_summary', 'export-summary', 'Export Summary', [
                    'rows' => 250,
                    'fields' => count($fieldNames),
                    'format' => 'XLSX',
                    'estimatedSize' => '1.4 MB',
                ]),
            ]),

            C::make('export_actions_row', 'row', 'Export Actions Row', [
                'columns' => 1,
                'cells' => [
                    [
                        'id' => 'export_actions_cell',
                        'span' => 12,
                        'components' => [
                            C::buttonGroup('export_actions', [
                                C::button('preview_export_btn', 'Validate & Preview Export', 'secondary'),
                                C::button('run_export_btn', 'Export', 'primary'),
                                C::button('cancel_export_btn', 'Cancel', 'light'),
                            ], 'Export Actions'),
                        ],
                        'style' => C::style([
                            'display' => 'flex',
                            'justifyContent' => 'flex-end',
                            'gap' => '8px',
                        ]),
                    ],
                ],
            ]),
        ]);
    }
}
