<?php

declare(strict_types=1);

defined('TYPO3') or die();

use B13\Container\Tca\ContainerConfiguration;
use B13\Container\Tca\Registry;
use TYPO3\CMS\Core\Utility\GeneralUtility;

// Register custom content element group label for the "New content element" wizard
$GLOBALS['TCA']['tt_content']['columns']['CType']['config']['itemGroups']['blockelement'] = 'Block Element';

// Grid containers (b13/container) – 1 to 12 columns
$containerRegistry = GeneralUtility::makeInstance(Registry::class);

// 1-column (full width)
$containerRegistry->configureContainer(
    (new ContainerConfiguration(
        'grid-1col',
        '1 Column',
        'Single column full width',
        [
            [['name' => 'Content', 'colPos' => 100]],
        ]
    ))
    ->setIcon('EXT:container/Resources/Public/Icons/container-1col.svg')
    ->setGroup('container')
);

// 2-column grid
$containerRegistry->configureContainer(
    (new ContainerConfiguration(
        'grid-2cols',
        '2 Column Grid',
        'Two columns side by side',
        [
            [
                ['name' => 'Col 1', 'colPos' => 201],
                ['name' => 'Col 2', 'colPos' => 202],
            ],
        ]
    ))
    ->setIcon('EXT:container/Resources/Public/Icons/container-2col.svg')
    ->setGroup('container')
);

// 3-column grid
$containerRegistry->configureContainer(
    (new ContainerConfiguration(
        'grid-3cols',
        '3 Column Grid',
        'Three columns side by side',
        [
            [
                ['name' => 'Col 1', 'colPos' => 301],
                ['name' => 'Col 2', 'colPos' => 302],
                ['name' => 'Col 3', 'colPos' => 303],
            ],
        ]
    ))
    ->setIcon('EXT:container/Resources/Public/Icons/container-3col.svg')
    ->setGroup('container')
);

// 4-column grid
$containerRegistry->configureContainer(
    (new ContainerConfiguration(
        'grid-4cols',
        '4 Column Grid',
        'Four columns side by side',
        [
            [
                ['name' => 'Col 1', 'colPos' => 401],
                ['name' => 'Col 2', 'colPos' => 402],
                ['name' => 'Col 3', 'colPos' => 403],
                ['name' => 'Col 4', 'colPos' => 404],
            ],
        ]
    ))
    ->setIcon('EXT:container/Resources/Public/Icons/container-3col.svg')
    ->setGroup('container')
);

// 5-column grid
$containerRegistry->configureContainer(
    (new ContainerConfiguration(
        'grid-5cols',
        '5 Column Grid',
        'Five columns side by side',
        [
            [
                ['name' => 'Col 1', 'colPos' => 501],
                ['name' => 'Col 2', 'colPos' => 502],
                ['name' => 'Col 3', 'colPos' => 503],
                ['name' => 'Col 4', 'colPos' => 504],
                ['name' => 'Col 5', 'colPos' => 505],
            ],
        ]
    ))
    ->setIcon('EXT:container/Resources/Public/Icons/container-3col.svg')
    ->setGroup('container')
);

// 6-column grid
$containerRegistry->configureContainer(
    (new ContainerConfiguration(
        'grid-6cols',
        '6 Column Grid',
        'Six columns side by side',
        [
            [
                ['name' => 'Col 1', 'colPos' => 601],
                ['name' => 'Col 2', 'colPos' => 602],
                ['name' => 'Col 3', 'colPos' => 603],
                ['name' => 'Col 4', 'colPos' => 604],
                ['name' => 'Col 5', 'colPos' => 605],
                ['name' => 'Col 6', 'colPos' => 606],
            ],
        ]
    ))
    ->setIcon('EXT:container/Resources/Public/Icons/container-3col.svg')
    ->setGroup('container')
);

// 7-column grid
$containerRegistry->configureContainer(
    (new ContainerConfiguration(
        'grid-7cols',
        '7 Column Grid',
        'Seven columns side by side',
        [
            [
                ['name' => 'Col 1', 'colPos' => 701],
                ['name' => 'Col 2', 'colPos' => 702],
                ['name' => 'Col 3', 'colPos' => 703],
                ['name' => 'Col 4', 'colPos' => 704],
                ['name' => 'Col 5', 'colPos' => 705],
                ['name' => 'Col 6', 'colPos' => 706],
                ['name' => 'Col 7', 'colPos' => 707],
            ],
        ]
    ))
    ->setIcon('EXT:container/Resources/Public/Icons/container-3col.svg')
    ->setGroup('container')
);

// 8-column grid
$containerRegistry->configureContainer(
    (new ContainerConfiguration(
        'grid-8cols',
        '8 Column Grid',
        'Eight columns side by side',
        [
            [
                ['name' => 'Col 1', 'colPos' => 801],
                ['name' => 'Col 2', 'colPos' => 802],
                ['name' => 'Col 3', 'colPos' => 803],
                ['name' => 'Col 4', 'colPos' => 804],
                ['name' => 'Col 5', 'colPos' => 805],
                ['name' => 'Col 6', 'colPos' => 806],
                ['name' => 'Col 7', 'colPos' => 807],
                ['name' => 'Col 8', 'colPos' => 808],
            ],
        ]
    ))
    ->setIcon('EXT:container/Resources/Public/Icons/container-3col.svg')
    ->setGroup('container')
);

// 9-column grid
$containerRegistry->configureContainer(
    (new ContainerConfiguration(
        'grid-9cols',
        '9 Column Grid',
        'Nine columns side by side',
        [
            [
                ['name' => 'Col 1', 'colPos' => 901],
                ['name' => 'Col 2', 'colPos' => 902],
                ['name' => 'Col 3', 'colPos' => 903],
                ['name' => 'Col 4', 'colPos' => 904],
                ['name' => 'Col 5', 'colPos' => 905],
                ['name' => 'Col 6', 'colPos' => 906],
                ['name' => 'Col 7', 'colPos' => 907],
                ['name' => 'Col 8', 'colPos' => 908],
                ['name' => 'Col 9', 'colPos' => 909],
            ],
        ]
    ))
    ->setIcon('EXT:container/Resources/Public/Icons/container-3col.svg')
    ->setGroup('container')
);

// 10-column grid
$containerRegistry->configureContainer(
    (new ContainerConfiguration(
        'grid-10cols',
        '10 Column Grid',
        'Ten columns side by side',
        [
            [
                ['name' => 'Col 1', 'colPos' => 1001],
                ['name' => 'Col 2', 'colPos' => 1002],
                ['name' => 'Col 3', 'colPos' => 1003],
                ['name' => 'Col 4', 'colPos' => 1004],
                ['name' => 'Col 5', 'colPos' => 1005],
                ['name' => 'Col 6', 'colPos' => 1006],
                ['name' => 'Col 7', 'colPos' => 1007],
                ['name' => 'Col 8', 'colPos' => 1008],
                ['name' => 'Col 9', 'colPos' => 1009],
                ['name' => 'Col 10', 'colPos' => 1010],
            ],
        ]
    ))
    ->setIcon('EXT:container/Resources/Public/Icons/container-3col.svg')
    ->setGroup('container')
);

// 11-column grid
$containerRegistry->configureContainer(
    (new ContainerConfiguration(
        'grid-11cols',
        '11 Column Grid',
        'Eleven columns side by side',
        [
            [
                ['name' => 'Col 1', 'colPos' => 1101],
                ['name' => 'Col 2', 'colPos' => 1102],
                ['name' => 'Col 3', 'colPos' => 1103],
                ['name' => 'Col 4', 'colPos' => 1104],
                ['name' => 'Col 5', 'colPos' => 1105],
                ['name' => 'Col 6', 'colPos' => 1106],
                ['name' => 'Col 7', 'colPos' => 1107],
                ['name' => 'Col 8', 'colPos' => 1108],
                ['name' => 'Col 9', 'colPos' => 1109],
                ['name' => 'Col 10', 'colPos' => 1110],
                ['name' => 'Col 11', 'colPos' => 1111],
            ],
        ]
    ))
    ->setIcon('EXT:container/Resources/Public/Icons/container-3col.svg')
    ->setGroup('container')
);

// 12-column grid
$containerRegistry->configureContainer(
    (new ContainerConfiguration(
        'grid-12cols',
        '12 Column Grid',
        'Twelve columns side by side',
        [
            [
                ['name' => 'Col 1', 'colPos' => 1201],
                ['name' => 'Col 2', 'colPos' => 1202],
                ['name' => 'Col 3', 'colPos' => 1203],
                ['name' => 'Col 4', 'colPos' => 1204],
                ['name' => 'Col 5', 'colPos' => 1205],
                ['name' => 'Col 6', 'colPos' => 1206],
                ['name' => 'Col 7', 'colPos' => 1207],
                ['name' => 'Col 8', 'colPos' => 1208],
                ['name' => 'Col 9', 'colPos' => 1209],
                ['name' => 'Col 10', 'colPos' => 1210],
                ['name' => 'Col 11', 'colPos' => 1211],
                ['name' => 'Col 12', 'colPos' => 1212],
            ],
        ]
    ))
    ->setIcon('EXT:container/Resources/Public/Icons/container-3col.svg')
    ->setGroup('container')
);

// ============================================================
// FlexForm registrations — b13/container grid containers
// ============================================================

// grid-2cols: column ratio + appearance settings
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPiFlexFormValue(
    '*',
    'FILE:EXT:testproject/Resources/Private/FlexForms/Container/GridTwoColumns.xml',
    'grid-2cols'
);
$GLOBALS['TCA']['tt_content']['types']['grid-2cols']['showitem'] .= ',--div--;Container Settings,pi_flexform';

// All other grid containers: shared appearance settings
$gridContainersWithBaseFlexForm = [
    'grid-1col',
    'grid-3cols',
    'grid-4cols',
    'grid-5cols',
    'grid-6cols',
    'grid-7cols',
    'grid-8cols',
    'grid-9cols',
    'grid-10cols',
    'grid-11cols',
    'grid-12cols',
];
foreach ($gridContainersWithBaseFlexForm as $cType) {
    \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPiFlexFormValue(
        '*',
        'FILE:EXT:testproject/Resources/Private/FlexForms/Container/GridContainer.xml',
        $cType
    );
    $GLOBALS['TCA']['tt_content']['types'][$cType]['showitem'] .= ',--div--;Container Settings,pi_flexform';
}

// ============================================================
// Custom fields for ALL content elements
// ============================================================

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
    'tt_content',
    [
        'tx_testproject_custom_class' => [
            'exclude' => 1,
            'label' => 'Extra CSS Class',
            'config' => [
                'type' => 'input',
                'size' => 50,
                'max' => 255,
                'eval' => 'trim',
                'placeholder' => 'e.g. my-section highlight-box',
            ],
        ],
    ]
);

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
    'tt_content',
    '--div--;Advanced, tx_testproject_custom_class',
    '',
    'after:editlock'
);
