{# UI Component Macros - Tailwind CSS with Dark Mode #}
{# Use these macros for consistent, maintainable UI components #}
{# All components support dark mode via dark: variants #}

{# ============================================================================
   BUTTONS
   Usage: {{ btn("Submit", variant="primary") }}
          {{ btn("Cancel", variant="secondary", size="sm") }}
          {{ btn_link("Delete", href="/delete", variant="danger") }}
   ============================================================================ #}

{# Base button classes - shared across all button variants #}
{% set _btn_base = "inline-flex items-center justify-center gap-2 font-medium rounded-md transition-colors focus:outline-none focus:ring-2 focus:ring-offset-2 dark:focus:ring-offset-slate-900 disabled:opacity-50 disabled:cursor-not-allowed" %}

{# Size variants #}
{% set _btn_sizes = {
    "sm": "px-2.5 py-1.5 text-xs",
    "md": "px-4 py-2 text-sm",
    "lg": "px-5 py-2.5 text-base"
} %}

{# Color variants with dark mode #}
{% set _btn_variants = {
    "primary": "bg-blue-600 text-white hover:bg-blue-700 focus:ring-blue-500 border border-transparent dark:bg-blue-500 dark:hover:bg-blue-600",
    "secondary": "bg-white text-slate-700 hover:bg-slate-50 focus:ring-slate-500 border border-slate-300 dark:bg-slate-800 dark:text-slate-200 dark:border-slate-600 dark:hover:bg-slate-700",
    "success": "bg-green-600 text-white hover:bg-green-700 focus:ring-green-500 border border-transparent dark:bg-green-500 dark:hover:bg-green-600",
    "danger": "bg-red-600 text-white hover:bg-red-700 focus:ring-red-500 border border-transparent dark:bg-red-500 dark:hover:bg-red-600",
    "warning": "bg-amber-500 text-white hover:bg-amber-600 focus:ring-amber-500 border border-transparent dark:bg-amber-400 dark:hover:bg-amber-500",
    "ghost": "bg-transparent text-slate-600 hover:bg-slate-100 focus:ring-slate-500 border border-transparent dark:text-slate-300 dark:hover:bg-slate-800",
    "link": "bg-transparent text-blue-600 hover:text-blue-700 hover:underline focus:ring-blue-500 border-none p-0 dark:text-blue-400 dark:hover:text-blue-300"
} %}

{# Button macro - renders a <button> element #}
{% macro btn(text, variant="primary", size="md", type="button", id="", name="", disabled=false, onclick="", classes="") %}
<button
    type="{{ type }}"
    {% if id %}id="{{ id }}"{% endif %}
    {% if name %}name="{{ name }}"{% endif %}
    {% if disabled %}disabled{% endif %}
    {% if onclick %}onclick="{{ onclick }}"{% endif %}
    class="{{ _btn_base }} {{ _btn_sizes[size] }} {{ _btn_variants[variant] }} {{ classes }}"
>{{ text }}</button>
{% endmacro %}

{# Button link macro - renders an <a> styled as a button #}
{% macro btn_link(text, href, variant="primary", size="md", target="", classes="") %}
<a
    href="{{ href }}"
    {% if target %}target="{{ target }}"{% endif %}
    class="{{ _btn_base }} {{ _btn_sizes[size] }} {{ _btn_variants[variant] }} {{ classes }}"
>{{ text }}</a>
{% endmacro %}

{# Icon button macro - button with just an icon #}
{% macro btn_icon(icon_html, variant="ghost", size="md", type="button", onclick="", title="", classes="") %}
<button
    type="{{ type }}"
    {% if onclick %}onclick="{{ onclick }}"{% endif %}
    {% if title %}title="{{ title }}" aria-label="{{ title }}"{% endif %}
    class="{{ _btn_base }} {{ _btn_sizes[size] }} {{ _btn_variants[variant] }} {{ classes }}"
>{{ icon_html|safe }}</button>
{% endmacro %}


{# ============================================================================
   FORMS
   Usage: {{ form_input("email", label="Email", type="email", required=true) }}
          {{ form_select("role", label="Role", options=roles) }}
          {{ form_textarea("query", label="SQL Query", rows=6) }}
   ============================================================================ #}

{# Input classes with dark mode #}
{% set _input_base = "w-full px-3 py-2 text-sm border border-slate-300 rounded-md bg-white text-slate-900 placeholder-slate-400 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 disabled:bg-slate-50 disabled:text-slate-500 dark:bg-slate-800 dark:border-slate-600 dark:text-white dark:placeholder-slate-500 dark:disabled:bg-slate-900 dark:disabled:text-slate-400" %}

{# Form group wrapper with dark mode #}
{% macro form_group(label="", id="", required=false, help_text="", error="") %}
<div class="mb-4">
    {% if label %}
    <label {% if id %}for="{{ id }}"{% endif %} class="block text-sm font-medium text-slate-700 dark:text-slate-200 mb-1.5">
        {{ label }}{% if required %}<span class="text-red-500 ml-0.5">*</span>{% endif %}
    </label>
    {% endif %}
    {{ caller() }}
    {% if help_text %}
    <p class="mt-1.5 text-xs text-slate-500 dark:text-slate-400">{{ help_text }}</p>
    {% endif %}
    {% if error %}
    <p class="mt-1.5 text-xs text-red-600 dark:text-red-400">{{ error }}</p>
    {% endif %}
</div>
{% endmacro %}

{# Text input #}
{% macro form_input(name, label="", type="text", value="", placeholder="", required=false, disabled=false, readonly=false, id="", help_text="", error="", classes="") %}
{% set input_id = id or name %}
{% call form_group(label=label, id=input_id, required=required, help_text=help_text, error=error) %}
<input
    type="{{ type }}"
    name="{{ name }}"
    id="{{ input_id }}"
    value="{{ value }}"
    {% if placeholder %}placeholder="{{ placeholder }}"{% endif %}
    {% if required %}required{% endif %}
    {% if disabled %}disabled{% endif %}
    {% if readonly %}readonly{% endif %}
    class="{{ _input_base }} {{ 'border-red-500' if error else '' }} {{ classes }}"
>
{% endcall %}
{% endmacro %}

{# Select dropdown #}
{% macro form_select(name, label="", options=[], value="", required=false, disabled=false, id="", help_text="", error="", classes="") %}
{% set input_id = id or name %}
{% call form_group(label=label, id=input_id, required=required, help_text=help_text, error=error) %}
<select
    name="{{ name }}"
    id="{{ input_id }}"
    {% if required %}required{% endif %}
    {% if disabled %}disabled{% endif %}
    class="{{ _input_base }} {{ 'border-red-500' if error else '' }} {{ classes }}"
>
    {% for opt in options %}
    {% if opt is mapping %}
    <option value="{{ opt.value }}" {% if opt.value == value %}selected{% endif %}>{{ opt.label }}</option>
    {% else %}
    <option value="{{ opt }}" {% if opt == value %}selected{% endif %}>{{ opt }}</option>
    {% endif %}
    {% endfor %}
</select>
{% endcall %}
{% endmacro %}

{# Textarea #}
{% macro form_textarea(name, label="", value="", placeholder="", rows=4, required=false, disabled=false, readonly=false, id="", help_text="", error="", classes="") %}
{% set input_id = id or name %}
{% call form_group(label=label, id=input_id, required=required, help_text=help_text, error=error) %}
<textarea
    name="{{ name }}"
    id="{{ input_id }}"
    rows="{{ rows }}"
    {% if placeholder %}placeholder="{{ placeholder }}"{% endif %}
    {% if required %}required{% endif %}
    {% if disabled %}disabled{% endif %}
    {% if readonly %}readonly{% endif %}
    class="{{ _input_base }} {{ 'border-red-500' if error else '' }} font-mono {{ classes }}"
>{{ value }}</textarea>
{% endcall %}
{% endmacro %}

{# Checkbox with dark mode #}
{% macro form_checkbox(name, label="", checked=false, disabled=false, id="", help_text="", classes="") %}
{% set input_id = id or name %}
<div class="flex items-start mb-4 {{ classes }}">
    <input
        type="checkbox"
        name="{{ name }}"
        id="{{ input_id }}"
        {% if checked %}checked{% endif %}
        {% if disabled %}disabled{% endif %}
        class="h-4 w-4 mt-0.5 text-blue-600 border-slate-300 rounded focus:ring-blue-500 dark:border-slate-600 dark:bg-slate-800"
    >
    <div class="ml-2">
        <label for="{{ input_id }}" class="text-sm font-medium text-slate-700 dark:text-slate-200">{{ label }}</label>
        {% if help_text %}
        <p class="text-xs text-slate-500 dark:text-slate-400">{{ help_text }}</p>
        {% endif %}
    </div>
</div>
{% endmacro %}


{# ============================================================================
   CARDS
   Usage: {% call card(title="My Card", subtitle="Optional subtitle") %}
            Content here
          {% endcall %}
   ============================================================================ #}

{% macro card(title="", subtitle="", classes="") %}
<div class="bg-white dark:bg-slate-800 border border-slate-200 dark:border-slate-700 rounded-lg shadow-sm mb-6 {{ classes }}">
    {% if title %}
    <div class="px-6 py-4 border-b border-slate-200 dark:border-slate-700">
        <h2 class="text-lg font-semibold text-slate-900 dark:text-white">{{ title }}</h2>
        {% if subtitle %}
        <p class="mt-1 text-sm text-slate-500 dark:text-slate-400">{{ subtitle }}</p>
        {% endif %}
    </div>
    {% endif %}
    <div class="p-6">
    {{ caller() }}
    </div>
</div>
{% endmacro %}

{% macro card_header(title="", subtitle="") %}
<div class="px-6 py-4 border-b border-slate-200 dark:border-slate-700 flex items-center justify-between">
    <div>
        <h2 class="text-lg font-semibold text-slate-900 dark:text-white">{{ title }}</h2>
        {% if subtitle %}
        <p class="mt-1 text-sm text-slate-500 dark:text-slate-400">{{ subtitle }}</p>
        {% endif %}
    </div>
    <div class="flex items-center gap-2">
        {{ caller() }}
    </div>
</div>
{% endmacro %}

{% macro card_body(classes="") %}
<div class="p-6 {{ classes }}">
    {{ caller() }}
</div>
{% endmacro %}

{# Stat card for dashboards #}
{% macro stat_card(label, value, icon="", trend="", trend_up=true, classes="") %}
<div class="bg-white dark:bg-slate-800 border border-slate-200 dark:border-slate-700 rounded-lg p-4 {{ classes }}">
    <div class="flex items-center justify-between">
        <div>
            <p class="text-sm font-medium text-slate-500 dark:text-slate-400">{{ label }}</p>
            <p class="mt-1 text-2xl font-bold text-slate-900 dark:text-white">{{ value }}</p>
            {% if trend %}
            <p class="mt-1 text-xs {% if trend_up %}text-green-600 dark:text-green-400{% else %}text-red-600 dark:text-red-400{% endif %}">
                {% if trend_up %}↑{% else %}↓{% endif %} {{ trend }}
            </p>
            {% endif %}
        </div>
        {% if icon %}
        <div class="text-slate-400 dark:text-slate-500">
            {{ icon|safe }}
        </div>
        {% endif %}
    </div>
</div>
{% endmacro %}


{# ============================================================================
   ALERTS
   Usage: {{ alert("Success!", variant="success") }}
          {{ alert("Error occurred", variant="error", dismissible=true) }}
   ============================================================================ #}

{% set _alert_variants = {
    "success": "bg-green-50 border-green-200 text-green-800 dark:bg-green-900/30 dark:border-green-800 dark:text-green-300",
    "error": "bg-red-50 border-red-200 text-red-800 dark:bg-red-900/30 dark:border-red-800 dark:text-red-300",
    "warning": "bg-amber-50 border-amber-200 text-amber-800 dark:bg-amber-900/30 dark:border-amber-800 dark:text-amber-300",
    "info": "bg-blue-50 border-blue-200 text-blue-800 dark:bg-blue-900/30 dark:border-blue-800 dark:text-blue-300"
} %}

{% macro alert(message, variant="info", title="", dismissible=false, id="") %}
<div
    {% if id %}id="{{ id }}"{% endif %}
    class="p-4 rounded-md border mb-4 {{ _alert_variants[variant] }}"
    role="alert"
>
    <div class="flex">
        <div class="flex-1">
            {% if title %}
            <p class="font-semibold">{{ title }}</p>
            {% endif %}
            <p class="text-sm">{{ message }}</p>
        </div>
        {% if dismissible %}
        <button
            type="button"
            onclick="this.parentElement.parentElement.remove()"
            class="ml-4 text-current opacity-50 hover:opacity-100"
            aria-label="Dismiss"
        >&times;</button>
        {% endif %}
    </div>
</div>
{% endmacro %}


{# ============================================================================
   BADGES / STATUS
   Usage: {{ badge("Active", variant="success") }}
          {{ status_dot("success") }} Active
   ============================================================================ #}

{% set _badge_variants = {
    "default": "bg-slate-100 text-slate-700 dark:bg-slate-700 dark:text-slate-300",
    "primary": "bg-blue-100 text-blue-800 dark:bg-blue-900/50 dark:text-blue-300",
    "success": "bg-green-100 text-green-800 dark:bg-green-900/50 dark:text-green-300",
    "warning": "bg-amber-100 text-amber-800 dark:bg-amber-900/50 dark:text-amber-300",
    "error": "bg-red-100 text-red-800 dark:bg-red-900/50 dark:text-red-300",
    "info": "bg-cyan-100 text-cyan-800 dark:bg-cyan-900/50 dark:text-cyan-300"
} %}

{% macro badge(text, variant="default", size="md", classes="") %}
{% set size_classes = "px-2 py-0.5 text-xs" if size == "sm" else "px-2.5 py-1 text-xs" %}
<span class="inline-flex items-center font-semibold rounded-full {{ size_classes }} {{ _badge_variants[variant] }} {{ classes }}">
    {{ text }}
</span>
{% endmacro %}

{% macro status_dot(status="default", classes="") %}
{% set colors = {
    "success": "bg-green-500",
    "error": "bg-red-500",
    "warning": "bg-amber-500",
    "info": "bg-blue-500",
    "default": "bg-slate-400 dark:bg-slate-500"
} %}
<span class="inline-block w-2 h-2 rounded-full {{ colors[status] }} {{ classes }}"></span>
{% endmacro %}


{# ============================================================================
   TABLES
   Usage: {% call table(headers=["Name", "Status", "Actions"]) %}
            <tr>...</tr>
          {% endcall %}
   ============================================================================ #}

{% macro table(headers=[], classes="") %}
<div class="overflow-x-auto">
    <table class="w-full text-sm text-slate-900 dark:text-slate-100 {{ classes }}">
        {% if headers %}
        <thead class="bg-slate-50 dark:bg-slate-800/50 text-slate-600 dark:text-slate-400 text-left">
            <tr>
                {% for header in headers %}
                <th class="px-4 py-3 font-medium">{{ header }}</th>
                {% endfor %}
            </tr>
        </thead>
        {% endif %}
        <tbody class="divide-y divide-slate-200 dark:divide-slate-700">
            {{ caller() }}
        </tbody>
    </table>
</div>
{% endmacro %}

{% macro table_cell(classes="") %}
<td class="px-4 py-3 {{ classes }}">{{ caller() }}</td>
{% endmacro %}

{% macro table_row(classes="") %}
<tr class="hover:bg-slate-50 dark:hover:bg-slate-800/50 {{ classes }}">{{ caller() }}</tr>
{% endmacro %}


{# ============================================================================
   EMPTY STATE
   Usage: {{ empty_state("No items found", description="Create your first item") }}
   ============================================================================ #}

{% macro empty_state(title, description="", icon="", action_text="", action_href="", classes="") %}
<div class="text-center py-12 px-6 {{ classes }}">
    {% if icon %}
    <div class="mx-auto mb-4 text-slate-300 dark:text-slate-600">{{ icon|safe }}</div>
    {% endif %}
    <h3 class="text-lg font-medium text-slate-900 dark:text-white mb-1">{{ title }}</h3>
    {% if description %}
    <p class="text-sm text-slate-500 dark:text-slate-400 mb-4">{{ description }}</p>
    {% endif %}
    {% if action_text and action_href %}
    {{ btn_link(action_text, action_href, variant="primary") }}
    {% endif %}
</div>
{% endmacro %}


{# ============================================================================
   LOADING
   Usage: {{ spinner() }}
          {{ skeleton_line() }}
          {{ skeleton_table(rows=5, cols=4) }}
   ============================================================================ #}

{% macro spinner(size="md", classes="") %}
{% set sizes = {"sm": "h-4 w-4", "md": "h-6 w-6", "lg": "h-8 w-8"} %}
<svg class="animate-spin {{ sizes[size] }} text-blue-600 dark:text-blue-400 {{ classes }}" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
    <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
    <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
{% endmacro %}

{% macro skeleton_line(width="full", classes="") %}
{% set widths = {"full": "w-full", "3/4": "w-3/4", "1/2": "w-1/2", "1/3": "w-1/3", "1/4": "w-1/4"} %}
<div class="animate-pulse {{ widths[width] }} h-4 bg-slate-200 dark:bg-slate-700 rounded {{ classes }}"></div>
{% endmacro %}

{% macro skeleton_card(classes="") %}
<div class="bg-white dark:bg-slate-800 border border-slate-200 dark:border-slate-700 rounded-lg p-6 mb-6 animate-pulse {{ classes }}">
    <div class="h-5 bg-slate-200 dark:bg-slate-700 rounded w-1/3 mb-4"></div>
    <div class="space-y-3">
        <div class="h-4 bg-slate-200 dark:bg-slate-700 rounded w-full"></div>
        <div class="h-4 bg-slate-200 dark:bg-slate-700 rounded w-5/6"></div>
        <div class="h-4 bg-slate-200 dark:bg-slate-700 rounded w-4/6"></div>
    </div>
</div>
{% endmacro %}

{# Skeleton table for loading states #}
{% macro skeleton_table(rows=5, cols=4, classes="") %}
<div class="bg-white dark:bg-slate-800 border border-slate-200 dark:border-slate-700 rounded-lg overflow-hidden animate-pulse {{ classes }}">
    {# Header #}
    <div class="bg-slate-50 dark:bg-slate-800/50 px-4 py-3 flex gap-4">
        {% for _ in range(cols) %}
        <div class="h-4 bg-slate-200 dark:bg-slate-700 rounded flex-1"></div>
        {% endfor %}
    </div>
    {# Rows #}
    {% for _ in range(rows) %}
    <div class="px-4 py-3 border-t border-slate-200 dark:border-slate-700 flex gap-4">
        {% for _ in range(cols) %}
        <div class="h-4 bg-slate-200 dark:bg-slate-700 rounded flex-1"></div>
        {% endfor %}
    </div>
    {% endfor %}
</div>
{% endmacro %}

{# Skeleton form for loading states #}
{% macro skeleton_form(fields=4, classes="") %}
<div class="bg-white dark:bg-slate-800 border border-slate-200 dark:border-slate-700 rounded-lg p-6 animate-pulse {{ classes }}">
    {% for _ in range(fields) %}
    <div class="mb-4">
        <div class="h-4 bg-slate-200 dark:bg-slate-700 rounded w-1/4 mb-2"></div>
        <div class="h-10 bg-slate-200 dark:bg-slate-700 rounded w-full"></div>
    </div>
    {% endfor %}
    <div class="flex justify-end gap-2 mt-6">
        <div class="h-10 bg-slate-200 dark:bg-slate-700 rounded w-24"></div>
        <div class="h-10 bg-slate-200 dark:bg-slate-700 rounded w-24"></div>
    </div>
</div>
{% endmacro %}

{# ============================================================================
   MODAL
   Usage: {% call modal(id="myModal", title="Modal Title") %}
            Modal content here
          {% endcall %}
   ============================================================================ #}

{% macro modal(id, title, size="md") %}
{% set sizes = {"sm": "max-w-md", "md": "max-w-lg", "lg": "max-w-2xl", "xl": "max-w-4xl"} %}
<div id="{{ id }}" class="fixed inset-0 z-50 hidden" aria-labelledby="{{ id }}-title" role="dialog" aria-modal="true">
    {# Backdrop #}
    <div class="fixed inset-0 bg-black/50 dark:bg-black/70 transition-opacity" onclick="closeModal('{{ id }}')"></div>

    {# Modal panel #}
    <div class="fixed inset-0 z-10 overflow-y-auto">
        <div class="flex min-h-full items-center justify-center p-4">
            <div class="relative bg-white dark:bg-slate-800 rounded-lg shadow-xl {{ sizes[size] }} w-full transform transition-all">
                {# Header #}
                <div class="flex items-center justify-between px-6 py-4 border-b border-slate-200 dark:border-slate-700">
                    <h3 id="{{ id }}-title" class="text-lg font-semibold text-slate-900 dark:text-white">{{ title }}</h3>
                    <button type="button" onclick="closeModal('{{ id }}')" class="text-slate-400 hover:text-slate-500 dark:text-slate-500 dark:hover:text-slate-400">
                        <svg class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
                        </svg>
                    </button>
                </div>
                {# Body #}
                <div class="px-6 py-4">
                    {{ caller() }}
                </div>
            </div>
        </div>
    </div>
</div>
{% endmacro %}

{# Modal footer for action buttons #}
{% macro modal_footer() %}
<div class="flex justify-end gap-3 px-6 py-4 border-t border-slate-200 dark:border-slate-700 bg-slate-50 dark:bg-slate-800/50 rounded-b-lg">
    {{ caller() }}
</div>
{% endmacro %}

{# ============================================================================
   INFO ROWS
   Usage: {{ info_row("Label", "Value") }}
   ============================================================================ #}

{% macro info_row(label, value, classes="") %}
<div class="flex justify-between items-center py-2 border-b border-slate-200 dark:border-slate-700 last:border-b-0 {{ classes }}">
    <span class="text-sm text-slate-500 dark:text-slate-400">{{ label }}</span>
    <span class="text-sm font-medium text-slate-900 dark:text-white">{{ value }}</span>
</div>
{% endmacro %}

{# ============================================================================
   TOOLBAR
   Usage: {% call toolbar() %}
            <div class="flex gap-2">buttons</div>
          {% endcall %}
   ============================================================================ #}

{% macro toolbar(classes="") %}
<div class="flex justify-between items-center mb-4 gap-4 {{ classes }}">
    {{ caller() }}
</div>
{% endmacro %}

{# ============================================================================
   FILTERS ROW
   Usage: For filter dropdowns and inputs
   ============================================================================ #}

{% macro filters(classes="") %}
<div class="flex flex-wrap gap-3 mb-4 {{ classes }}">
    {{ caller() }}
</div>
{% endmacro %}

{# ============================================================================
   CODE/PRE BLOCK
   Usage: {{ code_block(content) }}
   ============================================================================ #}

{% macro code_block(content, classes="") %}
<pre class="bg-slate-100 dark:bg-slate-900 text-slate-800 dark:text-slate-200 p-4 rounded-md text-sm font-mono overflow-x-auto {{ classes }}">{{ content }}</pre>
{% endmacro %}
