<div class="meta-manager">
    <div class="card shadow-sm border-0">
        <!-- Header -->
        <div class="card-header bg-white border-bottom py-3 d-flex justify-content-between align-items-center">
            <div>
                <h6 class="mb-0 fw-bold text-primary">
                    <i class="bi bi-tags-fill me-2"></i>Metadata Manager
                </h6>
                <small class="text-muted">Manage attributes for {{ class_basename($owner) }} #{{ $owner->id }}</small>
            </div>
            <div class="d-flex gap-2">
                <div class="input-group input-group-sm" style="width: 200px;">
                    <span class="input-group-text bg-light border-end-0"><i class="bi bi-search"></i></span>
                    <input type="text" wire:model.live="search" class="form-control border-start-0 ps-0" placeholder="Filter meta...">
                </div>
                @if(count($metaData) > 0)
                    <button wire:click="clearAll" wire:confirm="Are you sure you want to delete all metadata?" class="btn btn-sm btn-outline-danger">
                        <i class="bi bi-trash"></i>
                    </button>
                @endif
            </div>
        </div>

        <!-- Add New Meta Form -->
        <div class="card-body bg-light-subtle border-bottom px-4">
            <form wire:submit.prevent="addMeta" class="row g-2 align-items-end">
                <div class="col-md-5">
                    <label class="form-label small fw-semibold">Key</label>
                    <input type="text" wire:model="newKey" class="form-control form-control-sm @error('newKey') is-invalid @enderror" placeholder="e.g. theme_color">
                </div>
                <div class="col-md-5">
                    <label class="form-label small fw-semibold">Value</label>
                    <input type="text" wire:model="newValue" class="form-control form-control-sm @error('newValue') is-invalid @enderror" placeholder="e.g. dark">
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary btn-sm w-100">
                        <i class="bi bi-plus-lg me-1"></i> Add
                    </button>
                </div>
            </form>
        </div>

        <!-- Meta List -->
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="table-light">
                    <tr>
                        <th class="ps-4 border-0 small text-uppercase text-muted" style="width: 30%">Key</th>
                        <th class="border-0 small text-uppercase text-muted">Value</th>
                        <th class="pe-4 border-0 text-end small text-uppercase text-muted" style="width: 120px">Actions</th>
                    </tr>
                    </thead>
                    <tbody>
                    @forelse($metaData as $key => $value)
                        <tr wire:key="meta-row-{{ $key }}">
                            <td class="ps-4">
                                <code class="text-secondary fw-bold">{{ $key }}</code>
                            </td>
                            <td>
                                @if($isEditing === $key)
                                    <div class="input-group input-group-sm">
                                        <input type="text" wire:model="editingValue" class="form-control border-primary" autofocus>
                                        <button wire:click="saveEdit('{{ $key }}')" class="btn btn-success"><i class="bi bi-check-lg"></i></button>
                                        <button wire:click="cancelEdit" class="btn btn-outline-secondary"><i class="bi bi-x-lg"></i></button>
                                    </div>
                                @else
                                    <div class="d-flex align-items-center">
                                        <span class="text-dark">{{ is_array($value) ? json_encode($value) : $value }}</span>
                                        @if(is_numeric($value))
                                            <span class="badge rounded-pill bg-info-subtle text-info ms-2" style="font-size: 10px;">NUM</span>
                                        @elseif(is_bool($value) || in_array(strtolower($value), ['true', 'false']))
                                            <span class="badge rounded-pill bg-warning-subtle text-warning ms-2" style="font-size: 10px;">BOOL</span>
                                        @endif
                                    </div>
                                @endif
                            </td>
                            <td class="pe-4 text-end">
                                <div class="btn-group btn-group-sm shadow-none">
                                    <button wire:click="startEditing('{{ $key }}')" class="btn btn-link text-muted p-1" title="Edit">
                                        <i class="bi bi-pencil-square"></i>
                                    </button>
                                    <button wire:click="removeMeta('{{ $key }}')" wire:confirm="Remove this metadata?" class="btn btn-link text-danger p-1" title="Delete">
                                        <i class="bi bi-x-circle"></i>
                                    </button>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="3" class="text-center py-5">
                                <div class="text-muted mb-2"><i class="bi bi-inbox fs-2"></i></div>
                                <p class="mb-0 small">No metadata found for this record.</p>
                            </td>
                        </tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Footer / Status -->
        <div class="card-footer bg-white py-2">
            <div class="d-flex justify-content-between align-items-center">
                <span class="small text-muted" wire:loading.remove>
                    Total Keys: {{ count($metaData) }}
                </span>
                <span class="small text-primary" wire:loading>
                    <span class="spinner-border spinner-border-sm me-1" role="status"></span>
                    Syncing...
                </span>
            </div>
        </div>
    </div>
</div>