@php
    /**
     * The campaign as stored, with where each value came from.
     *
     * Rendered server side from the database rather than from the chat, so what
     * is shown and what would be published can never disagree.
     */
    $dot = fn (?string $source) => match ($source) {
        'user_stated' => ['bg-success', 'you said this'],
        'platform_returned' => ['bg-primary', 'returned by the platform'],
        'model_inferred' => ['bg-warning', 'worked out from context'],
        'fallback' => ['bg-danger', 'guessed, needs confirming'],
        default => ['bg-secondary-subtle', 'not set'],
    };

    $row = function (string $label, $value, ?string $source) use ($dot) {
        [$colour, $title] = $dot($source);

        return [$label, $value, $colour, $title];
    };
@endphp

{{-- A conversation can hold several campaigns. Without this the drawer shows
     one and the rest are only reachable by asking the agent to switch. --}}
@if (($campaigns ?? collect())->count() > 1)
    <div class="mb-campaign-switch">
        @foreach ($campaigns as $option)
            <button type="button"
                    class="mb-campaign-tab{{ $campaign && $option->id === $campaign->id ? ' active' : '' }}"
                    data-focus-campaign="{{ $option->id }}">
                {{ $option->name }}
                <span class="mb-campaign-tab-platform">{{ $option->platform }}</span>
            </button>
        @endforeach
    </div>
@endif

@if (! $campaign)
    <p class="text-body-secondary small mb-0">No campaign yet. It appears here as the chat fills it in.</p>
@else
    <div class="d-flex justify-content-between align-items-start mb-2">
        <strong>{{ $campaign->name }}</strong>
        <span class="badge {{ $campaign->status === 'published' ? 'text-bg-success' : 'text-bg-secondary' }}">
            {{ $campaign->status }}
        </span>
    </div>

    <table class="table table-sm mb-3">
        <tbody>
            @foreach ([
                $row('Platform', $campaign->platform, $sources['platform'] ?? null),
                $row('Objective', $campaign->objective, $sources['objective'] ?? null),
                $row('Landing page', $campaign->landing_url, $sources['landing_url'] ?? null),
                $row('Daily budget', $campaign->budget ? $campaign->currency.' '.$campaign->budget : null, $sources['budget'] ?? null),
                $row('Bidding', $campaign->bid_strategy, $sources['bid_strategy'] ?? null),
                $row('Target cost', $campaign->bid_amount, $sources['bid_amount'] ?? null),
                $row('Countries', implode(', ', $campaign->locations['countries'] ?? []), $sources['locations'] ?? null),
                $row('Placements', implode(', ', $campaign->placements ?? []), $sources['placements'] ?? null),
                $row('Ad account', $campaign->ad_account_id, $sources['ad_account_id'] ?? null),
                $row('Page', $campaign->page_id, $sources['page_id'] ?? null),
                $row('Pixel', $campaign->pixel_id, $sources['pixel_id'] ?? null),
            ] as [$label, $value, $colour, $title])
                <tr>
                    <td class="text-body-secondary" style="width: 38%;">
                        <span class="mb-dot {{ $colour }}" title="{{ $title }}"></span>
                        {{ $label }}
                    </td>
                    <td class="text-break">{{ filled($value) ? $value : '—' }}</td>
                </tr>
            @endforeach
        </tbody>
    </table>

    <div class="small text-body-secondary mb-1">
        Creatives: {{ $assets->count() }} attached
    </div>

    @if ($campaign->ads()->exists())
        <div class="small mb-3">
            @foreach ($campaign->ads as $ad)
                <div class="border rounded p-2 mb-1">
                    <div class="fw-semibold">{{ $ad->headline }}</div>
                    <div class="text-body-secondary">{{ $ad->primary_text }}</div>
                    <span class="badge text-bg-light">{{ $ad->cta }}</span>
                </div>
            @endforeach
        </div>
    @endif

    @if ($pending || $needsRebuild)
        <div class="alert alert-warning small py-2">
            <div class="fw-semibold mb-1">Not yet on the platform</div>

            @foreach ($pending as $field => $change)
                <div>
                    {{ str_replace('_', ' ', $field) }}:
                    <span class="text-decoration-line-through">{{ $change['from'] ?? 'not set' }}</span>
                    &rarr; <strong>{{ $change['to'] ?? 'not set' }}</strong>
                </div>
            @endforeach

            @foreach ($needsRebuild as $reason)
                <div>{{ $reason }}</div>
            @endforeach

            @if ($pending)
                <div class="mt-1">Ask in the chat to push these changes.</div>
            @endif
        </div>
    @endif

    @if ($campaign->external_ids)
        <div class="alert alert-success small py-2">
            Created on the platform:
            <div class="text-break font-monospace">{{ json_encode($campaign->external_ids) }}</div>
        </div>
    @endif

    @if ($campaign->last_error)
        <div class="alert alert-danger small py-2">{{ $campaign->last_error }}</div>
    @endif

    @if ($blocking)
        <div class="small">
            <div class="text-body-secondary mb-1">Still needed</div>
            <ul class="ps-3 mb-0">
                @foreach ($blocking as $reason)
                    <li>{{ $reason }}</li>
                @endforeach
            </ul>
        </div>
    @else
        <div class="alert alert-success small py-2 mb-0">Ready to publish.</div>
    @endif
@endif
