@extends('layouts.app')

@section('title', 'Plans')
@section('heading', 'Subscription Plans')

@section('content')
@php
    $symbol = config('app.currency_symbol', '₹');
@endphp
<div class="w-full">
    <x-superadmin.page-header
        eyebrow="Billing"
        title="Subscription plans"
        subtitle="Yearly team-size plans. Tenants are assigned to one of these plans."
    >
        <x-slot name="actions">
            <a href="{{ route('superadmin.plans.create') }}" class="inline-flex items-center rounded-xl bg-primary px-4 py-2.5 text-sm font-semibold text-white shadow-sm hover:opacity-95"><i class="bi bi-plus-lg me-1"></i>Add plan</a>
        </x-slot>
    </x-superadmin.page-header>

    @if($plans->isEmpty())
        <div class="rounded-2xl border border-dashed border-slate-200 bg-white py-12 text-center text-slate-500">
            No subscription plans defined.
            <a href="{{ route('superadmin.plans.create') }}" class="mt-2 block font-semibold text-primary hover:underline">Add your first plan</a>
        </div>
    @else
        <div class="grid gap-4 sm:grid-cols-2 xl:grid-cols-3">
            @foreach($plans as $plan)
                @php
                    $cardClass = $plan->is_popular ? 'ring-2 ring-primary/20 border-primary' : '';
                    $strike = $plan->displayStrikePrice();
                    $effective = $plan->effectiveYearlyPrice();
                    $seats = $plan->employee_limit !== null ? $plan->employee_limit : 'Unlimited';
                    $tenantCount = (int) ($plan->companies_count ?? 0);
                    $planSymbol = $plan->currency_symbol ?: $symbol;
                @endphp
                <article class="relative flex flex-col rounded-2xl border border-slate-200 bg-white p-5 shadow-sm {{ $cardClass }}">
                    <div class="absolute right-4 top-4 flex flex-wrap justify-end gap-1">
                        @if($plan->is_popular)
                            <span class="inline-flex rounded-full bg-primary px-2 py-0.5 text-[10px] font-bold uppercase tracking-wide text-white">Popular</span>
                        @endif
                        @if($plan->is_custom)
                            <span class="inline-flex rounded-full bg-slate-100 px-2 py-0.5 text-[10px] font-bold uppercase tracking-wide text-slate-600">Custom</span>
                        @endif
                        @if($plan->active)
                            <span class="inline-flex rounded-full bg-emerald-50 px-2 py-0.5 text-[10px] font-bold uppercase tracking-wide text-emerald-700 ring-1 ring-inset ring-emerald-200">Active</span>
                        @else
                            <span class="inline-flex rounded-full bg-slate-100 px-2 py-0.5 text-[10px] font-bold uppercase tracking-wide text-slate-500">Inactive</span>
                        @endif
                    </div>

                    <div class="pr-24">
                        <h3 class="text-lg font-semibold text-slate-900">{{ $plan->name }}</h3>
                        @if($plan->description)
                            <p class="mt-1 text-sm text-slate-500">{{ Str::limit($plan->description, 90) }}</p>
                        @endif
                    </div>

                    <div class="mt-5">
                        @if($strike)
                            <p class="text-sm text-slate-400 line-through">{{ $planSymbol }}{{ number_format($strike, 0) }}/yr</p>
                        @endif
                        <p class="text-3xl font-bold tracking-tight text-slate-900">
                            {{ $planSymbol }}{{ number_format($effective, 0) }}<span class="text-base font-medium text-slate-400">/yr</span>
                        </p>
                    </div>

                    <div class="mt-4 grid grid-cols-2 gap-2">
                        <div class="rounded-xl border border-slate-200 bg-slate-50/80 px-3 py-2">
                            <p class="text-[10px] font-semibold uppercase tracking-wide text-slate-400">Seats</p>
                            <p class="mt-0.5 text-sm font-semibold text-slate-900">{{ $seats }}</p>
                        </div>
                        <div class="rounded-xl border border-slate-200 bg-slate-50/80 px-3 py-2">
                            <p class="text-[10px] font-semibold uppercase tracking-wide text-slate-400">Tenants</p>
                            <p class="mt-0.5 text-sm font-semibold text-slate-900">{{ $tenantCount }}</p>
                        </div>
                    </div>

                    <div class="mt-auto mt-5 flex flex-wrap items-center gap-2 border-t border-slate-100 pt-4">
                        <a href="{{ route('superadmin.plans.edit', $plan) }}" class="btn-icon-action btn-icon-action--edit" title="Edit" aria-label="Edit"><i class="bi bi-pencil-square"></i></a>
                        <form action="{{ route('superadmin.plans.toggle-active', $plan) }}" method="post" class="inline">
                            @csrf
                            <button type="submit" class="inline-flex rounded-xl border border-slate-200 px-3 py-1.5 text-sm font-semibold text-slate-700 hover:bg-slate-50">
                                {{ $plan->active ? 'Deactivate' : 'Activate' }}
                            </button>
                        </form>
                        @if($tenantCount === 0)
                            <form action="{{ route('superadmin.plans.destroy', $plan) }}" method="post" class="inline" onsubmit="return confirm('Delete this plan?');">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="btn-icon-action btn-icon-action--delete" title="Delete" aria-label="Delete"><i class="bi bi-trash3"></i></button>
                            </form>
                        @endif
                    </div>
                </article>
            @endforeach
        </div>
    @endif
</div>
@endsection
