<?php

use Silvanus\includes\blocks\AcfBlockFields;

try {
    $fields = new AcfBlockFields('Latest Posts Block Grid', __FILE__);

    // Block Fields
    $fields->addContainerSettings();

    $fields->addChoiceField("text center", 'select', [
        'choices' => [
            'text-center' => 'Yes',
            '' => 'No',
        ],
        'default_value' => [''],
        'instructions' => 'Center the text inside each grid item.',
    ]);

    $fields->addGridOptions();

    $fields->addText('Main Header', [
        'default_value' => 'Latest Posts',
    ]);

    $fields->addGroup('frontend_controls', ['layout' => 'row'])
        ->addTrueFalse('enable_frontend_filters', [
            'ui' => 1,
            'default_value' => 0,
            'instructions' => 'If enabled, show filters on the frontend and update results via AJAX.',
        ])
        ->addUser('allowed_authors', [
            'label' => 'Allowed authors (frontend filter)',
            'multiple' => 1,
            'allow_null' => 1,
            'return_format' => 'id',
            'instructions' => 'Optional. If set, show these authors as clickable filters on the frontend.',
            'conditional_logic' => [
                [
                    [
                        'field' => 'field_enable_frontend_filters',
                        'operator' => '==',
                        'value' => '1',
                    ],
                ],
            ],
        ])
        ->addTrueFalse('enable_infinite_scroll_load_more', [
            'ui' => 1,
            'default_value' => 0,
            'instructions' => 'If enabled, results can be loaded via a button or on scroll.',
        ])
        ->addSelect('load_more_mode', [
            'choices' => [
                'button' => 'Show more button',
                'scroll' => 'Load more on scroll',
            ],
            'default_value' => 'button',
            'ui' => 1,
            'conditional_logic' => [
                [
                    [
                        'field' => 'field_enable_infinite_scroll_load_more',
                        'operator' => '==',
                        'value' => '1',
                    ],
                ],
            ],
        ])
        ->addText('load_more_button_text', [
            'default_value' => 'Show more',
            'conditional_logic' => [
                [
                    [
                        'field' => 'field_enable_infinite_scroll_load_more',
                        'operator' => '==',
                        'value' => '1',
                    ],
                    [
                        'field' => 'field_load_more_mode',
                        'operator' => '==',
                        'value' => 'button',
                    ],
                ],
            ],
        ])
        ->addNumber('posts_per_page', [
            'default_value' => 6,
            'min' => 1,
            'max' => 50,
            'step' => 1,
            'instructions' => 'How many items to fetch per request (and on initial render).',
            'conditional_logic' => [
                [
                    [
                        'field' => 'field_enable_infinite_scroll_load_more',
                        'operator' => '==',
                        'value' => '1',
                    ],
                ],
            ],
        ])
        ->endGroup();

    $fields->addTrueFalse('use_manual_posts', [
        'label' => 'Use manually selected posts',
        'default_value' => 0,
        'ui' => 1,
        'instructions' => 'If enabled, the grid will use the selected posts below (in order) instead of the query settings.',
    ]);

    $fields->addRelationship('manual_posts', [
        'label' => 'Manual posts',
        'post_type' => ['post', 'page', ...array_keys(get_post_types(['public' => true], 'objects'))],
        'filters' => ['search', 'post_type', 'taxonomy'],
        'return_format' => 'id',
        'elements' => ['featured_image'],
        'min' => 0,
        'max' => 0,
        'instructions' => 'Select posts/pages to display (order is preserved).',
        'conditional_logic' => [
            [
                [
                    'field' => 'field_use_manual_posts',
                    'operator' => '==',
                    'value' => '1',
                ],
            ],
        ],
    ]);

    // Query Settings
    $fields->addGroup('query', ['layout' => 'row'])
        ->addChoiceField('post_type', 'select', [
            'choices' => [
                'post' => 'Posts',
                'page' => 'Pages',
                'other' => 'Other (enter slug)',
            ],
            'default_value' => 'post',
            'ui' => 1,
            'instructions' => 'Select which post type to pull from.',
        ])
        ->addText('post_type_slug', [
            'instructions' => 'Only used when Post type = Other. Example: "event".',
        ])
        ->addNumber('max_posts', [
            'default_value' => 6,
            'min' => 1,
            'max' => 200,
            'step' => 1,
            'instructions' => 'Maximum number of posts to show in total.',
        ])
        ->addSelect('taxonomy', [
            'allow_null' => 1,
            'ui' => 1,
            'placeholder' => 'No taxonomy filter',
            'choices' => [],
            'instructions' => 'Optional. Select a taxonomy for the chosen post type.',
        ])
        ->addSelect('terms', [
            'multiple' => 1,
            'ui' => 1,
            'allow_null' => 1,
            'placeholder' => 'Select terms',
            'choices' => [],
            'instructions' => 'Optional. Select one or more terms from the selected taxonomy.',
        ])
        ->addUser('author', [
            'role' => '',
            'multiple' => 0,
            'allow_null' => 1,
            'return_format' => 'id',
            'instructions' => 'Optional. Filter by author.',
        ])
        ->addDatePicker('date from', [
            'display_format' => 'Y-m-d',
            'return_format' => 'Y-m-d',
            'first_day' => 1,
            'instructions' => 'Optional. Only show posts on/after this date.',
        ])
        ->addDatePicker('date to', [
            'display_format' => 'Y-m-d',
            'return_format' => 'Y-m-d',
            'first_day' => 1,
            'instructions' => 'Optional. Only show posts on/before this date.',
        ])
        ->endGroup();

    $customPostTypes = get_post_types(['public' => true], 'objects');

    $fields->addText('block_col_class', [
        'label' => 'Column classes',
        'default_value' => 'col',
        'instructions' => 'Optional. Add additional classes to each grid item column.',
    ]);

    // Optional per-post overrides
    $fields->addRepeater('post overrides', [
        'layout' => 'block',
        'button_label' => 'Add Override',
        'instructions' => 'Optional. Override background and/or description for specific posts displayed in this grid.',
    ])
        ->addPostObject('post', [
            'post_type' => ['post', 'page', ...array_keys($customPostTypes)],
            'return_format' => 'id',
            'multiple' => 0,
            'allow_null' => 0,
        ])
        ->addImage('background_image', [
            'return_format' => 'id',
            'preview_size' => 'medium',
            'library' => 'all',
            'mime_types' => 'jpeg,jpg,png,gif,webp,svg',
            'instructions' => 'Optional. Background image for this post card.',
        ])
        ->addWysiwyg('description', [
            'tabs' => 'all',
            'media_upload' => 0,
            'instructions' => 'Optional. If set, replaces the post excerpt for this card.',
        ])
        ->addSelect('text_vertical_alignment', [
            'choices' => [
                '' => 'Default',
                'align-content-start' => 'Top',
                'align-content-center' => 'Center',
                'align-content-end' => 'Bottom',
            ],
            'ui' => 1,
            'instructions' => 'Optional. Override the vertical alignment of the text for this post card.',
        ])
        ->addTrueFalse('hide_read_more_button', [
            'ui' => 1,
            'default_value' => 0,
            'instructions' => 'If enabled, the "Read More" button will be hidden for this post card and instead the grid item is wrapped in the permalink.',
        ])
        ->endRepeater();

    return $fields->build();
} catch (\StoutLogic\AcfBuilder\FieldNameCollisionException $e) {
    // no-op
}
