<?php
/**
 * Block Registry Field Model
 * Individual field definitions within a block
 */
namespace Mtc\Modules\BlockRegistry\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class BlockRegistryField extends Model
{
    protected $table = 'block_registry_fields';

    protected $fillable = [
        'block_id',
        'parent_field_id',
        'name',
        'label',
        'type',
        'order_index',
        'required',
        'css_class',
        'settings',
        'options',
        'regex',
        'mime',
        'default_value',
        'info',
        'is_repeater',
        'repeater_min',
        'repeater_max',
        'repeater_text',
    ];

    protected $casts = [
        'block_id' => 'integer',
        'parent_field_id' => 'integer',
        'order_index' => 'integer',
        'required' => 'boolean',
        'is_repeater' => 'boolean',
        'repeater_min' => 'integer',
        'repeater_max' => 'integer',
    ];

    /**
     * Available field types (matching existing pagedata types)
     */
    public const FIELD_TYPES = [
        'text' => 'Text Input',
        'textarea' => 'Text Box',
        'checkbox' => 'Checkbox',
        'select' => 'Select Input',
        'image' => 'Image Input',
        'file' => 'File Input',
        'datetime' => 'Date',
        'colorpicker' => 'Color Picker',
        'product_selector' => 'Product Selector',
        'page_selector' => 'Page Selector',
        'link' => 'Link',
        'repeater' => 'Repeater',
    ];

    /**
     * Get field types available for sub-fields (excludes repeater to prevent nesting)
     */
    public static function getSubFieldTypes(): array
    {
        $types = self::FIELD_TYPES;
        unset($types['repeater']);
        return $types;
    }

    /**
     * Get the parent block
     */
    public function block(): BelongsTo
    {
        return $this->belongsTo(BlockRegistry::class, 'block_id');
    }

    /**
     * Get sub-fields for repeater fields
     */
    public function subFields(): HasMany
    {
        return $this->hasMany(self::class, 'parent_field_id')->orderBy('order_index');
    }

    /**
     * Get parent field (for sub-fields)
     */
    public function parentField(): BelongsTo
    {
        return $this->belongsTo(self::class, 'parent_field_id');
    }

    /**
     * Check if this is a sub-field
     */
    public function isSubField(): bool
    {
        return $this->parent_field_id !== null;
    }

    /**
     * Check if this is a repeater field
     */
    public function isRepeater(): bool
    {
        return $this->type === 'repeater';
    }

    /**
     * Get display label (falls back to name if label is empty)
     */
    public function getDisplayLabelAttribute(): string
    {
        return $this->label ?: $this->name;
    }

    /**
     * Convert field to pagedata format
     */
    public function toPagedataFormat(): array
    {
        $data = [
            'name' => $this->name,
            'type' => $this->type,
            'order' => $this->order_index,
        ];

        if ($this->required) {
            $data['required'] = 1;
        }
        if ($this->css_class) {
            $data['css_class'] = $this->css_class;
        }
        if ($this->settings) {
            $data['settings'] = $this->settings;
        }
        if ($this->options) {
            $data['options'] = $this->options;
        }
        if ($this->regex) {
            $data['regex'] = $this->regex;
        }
        if ($this->mime) {
            $data['mime'] = $this->mime;
        }
        if ($this->info) {
            $data['info'] = $this->info;
        }

        return $data;
    }

    /**
     * Create field from pagedata format
     */
    public static function fromPagedataFormat(array $data, int $blockId): self
    {
        return new self([
            'block_id' => $blockId,
            'name' => $data['name'] ?? '',
            'label' => $data['name'] ?? '',
            'type' => $data['type'] ?? 'text',
            'order_index' => $data['order'] ?? 0,
            'required' => !empty($data['required']),
            'css_class' => $data['css_class'] ?? null,
            'settings' => $data['settings'] ?? null,
            'options' => $data['options'] ?? null,
            'regex' => $data['regex'] ?? null,
            'mime' => $data['mime'] ?? null,
            'info' => $data['info'] ?? null,
            'default_value' => $data['value'] ?? null,
        ]);
    }
}
