@extends('layouts.app')

@section('title', 'View Post - Social Scheduler')

@section('content')
<div class="topbar">
    <h4 class="mb-0 fw-bold"><i class="bi bi-eye me-2 text-primary"></i>Post Details</h4>
    <div>
        <a href="{{ route('posts.index') }}" class="btn btn-outline-secondary me-2">
            <i class="bi bi-arrow-left me-1"></i> Back
        </a>
        @if(in_array($post->status, ['draft', 'scheduled']))
        <a href="{{ route('posts.edit', $post) }}" class="btn btn-primary">
            <i class="bi bi-pencil me-1"></i> Edit
        </a>
        @endif
    </div>
</div>

<div class="row g-4">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <span>Post Content</span>
                <span class="badge {{ $post->status_badge_class }}">{{ $post->status_label }}</span>
            </div>
            <div class="card-body">
                <div class="post-content fs-5 mb-4">{{ $post->content }}</div>

                @if($post->media_path)
                <div class="mb-4">
                    <h6 class="fw-semibold mb-2"><i class="bi bi-paperclip me-1"></i>Media</h6>
                    @if($post->media_type === 'video')
                        <video src="{{ $post->media_url }}" controls class="w-100" style="max-height: 400px; border-radius: 8px;"></video>
                    @else
                        <img src="{{ $post->media_url }}" class="img-fluid" style="max-height: 400px; border-radius: 8px;" alt="Post media">
                    @endif
                </div>
                @endif

                <div class="row g-3 text-muted" style="font-size: 0.875rem;">
                    <div class="col-md-6">
                        <strong>Created:</strong> {{ $post->created_at->format('F d, Y H:i:s') }}
                    </div>
                    <div class="col-md-6">
                        <strong>Updated:</strong> {{ $post->updated_at->format('F d, Y H:i:s') }}
                    </div>
                    @if($post->scheduled_at)
                    <div class="col-md-6">
                        <strong>Scheduled:</strong> {{ $post->scheduled_at->format('F d, Y H:i:s') }}
                    </div>
                    @endif
                    @if($post->published_at)
                    <div class="col-md-6">
                        <strong>Published:</strong> {{ $post->published_at->format('F d, Y H:i:s') }}
                    </div>
                    @endif
                </div>
            </div>
        </div>

        @if($post->publish_log)
        <div class="card mt-4">
            <div class="card-header">
                <i class="bi bi-journal-text me-2"></i>Publish Log
            </div>
            <div class="card-body">
                <pre class="mb-0" style="font-size: 0.8rem;"><code>{{ json_encode(json_decode($post->publish_log), JSON_PRETTY_PRINT) }}</code></pre>
            </div>
        </div>
        @endif
    </div>

    <div class="col-lg-4">
        <div class="card">
            <div class="card-header">
                <i class="bi bi-share me-2"></i>Platform Status
            </div>
            <div class="card-body">
                @foreach($post->postPlatforms as $pp)
                <div class="d-flex align-items-center gap-3 mb-3 pb-3" style="border-bottom: 1px solid #f0f0f0;">
                    <span class="platform-icon flex-shrink-0" style="background-color: {{ $pp->socialAccount->platform_color }};">
                        <i class="bi {{ $pp->socialAccount->platform_icon }}"></i>
                    </span>
                    <div class="flex-grow-1">
                        <div class="fw-semibold">{{ ucfirst($pp->socialAccount->platform_name) }}</div>
                        <div class="text-muted" style="font-size: 0.8rem;">{{ $pp->socialAccount->account_name }}</div>
                    </div>
                    <span class="badge {{ match($pp->publish_status) {
                        'published' => 'bg-success',
                        'pending' => 'bg-warning',
                        'failed' => 'bg-danger',
                        default => 'bg-secondary'
                    } }}">
                        {{ ucfirst($pp->publish_status) }}
                    </span>
                </div>
                @endforeach
            </div>
        </div>

        @if($post->status === 'scheduled')
        <div class="card mt-4">
            <div class="card-body">
                <form action="{{ route('posts.publish-now', $post) }}" method="POST">
                    @csrf
                    <button type="submit" class="btn btn-success w-100"
                            onclick="return confirm('Publish this post immediately?')">
                        <i class="bi bi-send me-2"></i>Publish Now
                    </button>
                </form>
            </div>
        </div>
        @endif
    </div>
</div>
@endsection
