<?php

use Silvanus\includes\blocks\AcfBlockFields;

try {
    $fields = new AcfBlockFields('Testimonial Slider', __FILE__);

    $fields->addContainerSettings();

    $fields->addText('main header', [
        'label' => 'Heading',
        'required' => 0,
    ]);

    $fields->addGroup('source', [
        'label' => 'Source',
        'layout' => 'block',
    ]);

    $fields->addTrueFalse('use manual', [
        'label' => 'Select testimonials manually',
        'ui' => 1,
        'default_value' => 0,
    ]);

    $fields->addRelationship('manual testimonials', [
        'label' => 'Manual testimonials',
        'post_type' => ['testimonial'],
        'filters' => ['search'],
        'return_format' => 'id',
        'min' => 0,
        'max' => 0,
        'conditional_logic' => [
            [
                [
                    'field' => 'field_testimonial_slider_source_use_manual',
                    'operator' => '==',
                    'value' => 1,
                ],
            ],
        ],
    ]);

    $fields->addNumber('max posts', [
        'label' => 'Max testimonials (auto)',
        'default_value' => 6,
        'min' => 1,
        'max' => 50,
        'conditional_logic' => [
            [
                [
                    'field' => 'field_testimonial_slider_source_use_manual',
                    'operator' => '!=',
                    'value' => 1,
                ],
            ],
        ],
    ]);

    $fields->endGroup();

    $fields->addGroup('display', [
        'label' => 'Display fields',
        'layout' => 'block',
        'instructions' => 'Choose which testimonial fields to render per slide. List is populated from content/types/testimonial.cpt.php',
    ]);

    $fields->addCheckbox('fields_to_show', [
        'label' => 'Fields to show',
        'choices' => [],
        'layout' => 'vertical',
        'toggle' => 1,
        'return_format' => 'value',
        'default_value' => ['quote', 'client_name', 'client_company'],
    ]);

    $fields->endGroup();

    $fields->addGroup('field_mapping', [
        'label' => 'Testimonial field mapping',
        'layout' => 'block',
        'instructions' => 'Enter ACF field names (meta keys) that exist on the Testimonial posts. Leave blank to ignore.',
    ]);

    $fields->addText('quote_field', [
        'label' => 'Quote field name',
        'default_value' => 'quote',
        'required' => 0,
        'instructions' => 'Example: quote',
    ]);

    $fields->addText('author_name_field', [
        'label' => 'Author name field name',
        'default_value' => 'author_name',
        'required' => 0,
    ]);

    $fields->addText('author_role_field', [
        'label' => 'Author role field name',
        'default_value' => 'author_role',
        'required' => 0,
    ]);

    $fields->addText('author_company_field', [
        'label' => 'Author company field name',
        'default_value' => 'author_company',
        'required' => 0,
    ]);

    $fields->addText('rating_field', [
        'label' => 'Rating field name',
        'default_value' => 'rating',
        'required' => 0,
        'instructions' => 'Should be numeric (e.g. 1–5).',
    ]);

    $fields->endGroup();

    $fields->addGroup('ui', [
        'label' => 'Slider UI',
        'layout' => 'block',
    ]);

    $fields->addTrueFalse('show_dots', [
        'label' => 'Show dot navigation',
        'ui' => 1,
        'default_value' => 1,
    ]);

    $fields->addTrueFalse('show_arrows', [
        'label' => 'Show left/right arrows',
        'ui' => 1,
        'default_value' => 1,
    ]);

    $fields->endGroup();

    $fields->addGroup('behavior', [
        'label' => 'Behavior',
        'layout' => 'block',
    ]);

    $fields->addTrueFalse('autoplay', [
        'label' => 'Autoplay',
        'ui' => 1,
        'default_value' => 0,
    ]);

    $fields->addNumber('interval_ms', [
        'label' => 'Autoplay interval (ms)',
        'default_value' => 5000,
        'min' => 1000,
        'step' => 500,
        'conditional_logic' => [
            [
                [
                    'field' => 'field_testimonial_slider_behavior_autoplay',
                    'operator' => '==',
                    'value' => 1,
                ],
            ],
        ],
    ]);

    $fields->endGroup();

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