@extends('layouts.business')

@section('title', 'Marketing & Campaigns')

@section('content')
<div class="container-fluid py-4">
    <!-- Header -->
    <div class="row mb-4">
        <div class="col-md-8">
            <h1 class="h2 fw-bold mb-0">
                <i class="fas fa-bullhorn me-2"></i>Marketing & Campaigns
            </h1>
            <p class="text-muted">Create and manage promotional campaigns to boost your visibility</p>
        </div>
    </div>

    <!-- Check Business Subscription Status -->
    @if(!$subscription)
    <div class="alert alert-warning alert-dismissible fade show mb-4" role="alert">
        <div class="d-flex align-items-center">
            <i class="fas fa-exclamation-triangle me-3 fs-5"></i>
            <div class="flex-grow-1">
                <h5 class="alert-heading mb-1">Business Plan Required</h5>
                <p class="mb-0">You don't have an active business subscription. Subscribe to a business plan that includes campaigns to create promotional content.</p>
            </div>
            <a href="{{ route('business.subscription.plans', ['type' => 'business']) }}" class="btn btn-warning btn-sm ms-2">
                <i class="fas fa-shopping-cart me-1"></i>Subscribe Now
            </a>
        </div>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    @endif

    <!-- Campaign Limit Alert -->
    @if($subscription && !$canCreateCampaigns)
    <div class="alert alert-info alert-dismissible fade show mb-4" role="alert">
        <i class="fas fa-info-circle me-2"></i>
        <strong>Campaign Limit:</strong> Your current plan doesn't include campaign creation. Upgrade to a plan that includes promotional campaigns.
        <a href="{{ route('business.subscription.plans', ['type' => 'business']) }}" class="alert-link ms-2">View Premium Plans</a>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    @elseif($subscription && $canCreateCampaigns)
    <div class="alert alert-success alert-dismissible fade show mb-4" role="alert">
        <i class="fas fa-check-circle me-2"></i>
        <strong>Campaign Allowance:</strong> {{ $campaignLimit - $activeCampaignsThisMonth }}/{{ $campaignLimit }} campaigns remaining this month
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    @endif

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card shadow-sm border-0">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <p class="text-muted small fw-bold mb-1">ACTIVE CAMPAIGNS</p>
                            <h3 class="mb-0">{{ $activeCampaigns ?? 0 }}</h3>
                        </div>
                        <div class="bg-primary bg-opacity-10 rounded-circle p-3">
                            <i class="fas fa-play-circle text-primary fs-5"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-md-3">
            <div class="card shadow-sm border-0">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <p class="text-muted small fw-bold mb-1">TOTAL IMPRESSIONS</p>
                            <h3 class="mb-0">{{ number_format($totalImpressions ?? 0) }}</h3>
                        </div>
                        <div class="bg-info bg-opacity-10 rounded-circle p-3">
                            <i class="fas fa-eye text-info fs-5"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-md-3">
            <div class="card shadow-sm border-0">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <p class="text-muted small fw-bold mb-1">TOTAL CLICKS</p>
                            <h3 class="mb-0">{{ number_format($totalClicks ?? 0) }}</h3>
                        </div>
                        <div class="bg-success bg-opacity-10 rounded-circle p-3">
                            <i class="fas fa-mouse text-success fs-5"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-md-3">
            <div class="card shadow-sm border-0">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <p class="text-muted small fw-bold mb-1">TOTAL SPENT</p>
                            <h3 class="mb-0">AED {{ number_format($totalSpent ?? 0, 2) }}</h3>
                        </div>
                        <div class="bg-warning bg-opacity-10 rounded-circle p-3">
                            <i class="fas fa-coins text-warning fs-5"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Quick Actions -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card shadow-sm border-0">
                <div class="card-body">
                    <h5 class="fw-bold mb-3">Quick Actions</h5>
                    <div class="row g-3">
                        <div class="col-md-4">
                            <a href="{{ route('business.marketing.campaigns') }}" class="btn btn-outline-primary w-100">
                                <i class="fas fa-list me-2"></i>My Campaigns
                            </a>
                        </div>
                        @if($canCreateCampaigns && $activeCampaignsThisMonth < $campaignLimit)
                        <div class="col-md-4">
                            <a href="{{ route('business.marketing.campaigns.create') }}" class="btn btn-primary w-100">
                                <i class="fas fa-plus-circle me-2"></i>Create Campaign
                            </a>
                        </div>
                        @else
                        <div class="col-md-4">
                            <button class="btn btn-primary w-100" disabled title="Campaign limit reached this month">
                                <i class="fas fa-lock me-2"></i>Campaign Limit Reached
                            </button>
                        </div>
                        @endif
                        <div class="col-md-4">
                            <a href="{{ route('business.subscription.index') }}" class="btn btn-outline-info w-100">
                                <i class="fas fa-cog me-2"></i>Manage Plans
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Current Subscription Status -->
    @if($subscription && $plan)
    <div class="row mb-4">
        <div class="col-12">
            <div class="card shadow-sm border-0 border-start border-success border-4">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col-md-8">
                            <h5 class="fw-bold mb-2">
                                <i class="fas fa-check-circle text-success me-2"></i>Active Business Plan
                            </h5>
                            <p class="text-muted mb-1">
                                <strong>Plan:</strong> {{ $plan->name }}
                            </p>
                            <p class="text-muted mb-0">
                                <strong>Features:</strong>
                                {{ $plan->max_venues ?? '∞' }} Venues •
                                {{ $plan->max_offers ?? '∞' }} Offers •
                                {{ $plan->monthly_campaigns ?? 0 }} Monthly Campaigns
                            </p>
                            <p class="text-muted mb-0 mt-2">
                                Expires: {{ $subscription->end_date->format('M d, Y') }}
                                <span class="badge bg-success ms-2">{{ $subscription->end_date->diffForHumans() }}</span>
                            </p>
                        </div>
                        <div class="col-md-4 text-end">
                            <a href="{{ route('business.subscription.index') }}" class="btn btn-outline-primary">
                                <i class="fas fa-arrow-right me-1"></i>Manage Subscription
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    @endif

    <!-- Recent Campaign Performance Section -->
    @if($recentCampaigns && count($recentCampaigns) > 0)
    <div class="row mt-5">
        <div class="col-12">
            <h4 class="fw-bold mb-3">Recent Campaign Performance</h4>
        </div>
    </div>

    <div class="row">
        @foreach($recentCampaigns as $campaign)
        <div class="col-lg-6 mb-4">
            <div class="card shadow-sm border-0">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-start mb-3">
                        <div>
                            <h6 class="fw-bold mb-1">{{ $campaign->name }}</h6>
                            <small class="text-muted">{{ $campaign->created_at->diffForHumans() }}</small>
                        </div>
                        <span class="badge bg-{{ $campaign->status === 'active' ? 'success' : ($campaign->status === 'paused' ? 'warning' : 'secondary') }}">
                            {{ ucfirst($campaign->status) }}
                        </span>
                    </div>

                    <div class="row g-3">
                        <div class="col-6">
                            <small class="text-muted d-block">Impressions</small>
                            <h6 class="mb-0">{{ number_format($campaign->metrics['impressions'] ?? 0) }}</h6>
                        </div>
                        <div class="col-6">
                            <small class="text-muted d-block">Clicks</small>
                            <h6 class="mb-0">{{ number_format($campaign->metrics['clicks'] ?? 0) }}</h6>
                        </div>
                        <div class="col-6">
                            <small class="text-muted d-block">Budget</small>
                            <h6 class="mb-0">AED {{ number_format($campaign->budget, 2) }}</h6>
                        </div>
                        <div class="col-6">
                            <small class="text-muted d-block">CTR</small>
                            @php
                                $impressions = $campaign->metrics['impressions'] ?? 1;
                                $clicks = $campaign->metrics['clicks'] ?? 0;
                                $ctr = $impressions > 0 ? (($clicks / $impressions) * 100) : 0;
                            @endphp
                            <h6 class="mb-0">{{ number_format($ctr, 2) }}%</h6>
                        </div>
                    </div>

                    <div class="mt-3">
                        <a href="{{ route('marketing.campaigns.edit', $campaign->id) }}" class="btn btn-sm btn-outline-primary">
                            <i class="fas fa-edit me-1"></i>Edit Campaign
                        </a>
                    </div>
                </div>
            </div>
        </div>
        @endforeach
    </div>
    @else
    <div class="row mt-5">
        <div class="col-12">
            <div class="card shadow-sm border-0">
                <div class="card-body text-center py-5">
                    <i class="fas fa-inbox text-muted" style="font-size: 48px; opacity: 0.5;"></i>
                    <p class="text-muted mt-3">No campaigns yet</p>
                    @if($canCreateCampaigns && $activeCampaignsThisMonth < $campaignLimit)
                    <a href="{{ route('business.marketing.campaigns.create') }}" class="btn btn-primary mt-3">
                        <i class="fas fa-plus-circle me-2"></i>Create Your First Campaign
                    </a>
                    @endif
                </div>
            </div>
        </div>
    </div>
    @endif
</div>
@endsection

