@extends('layouts.app')

@section('title', $tenant->name)
@section('page-title', $tenant->name)
@section('page-subtitle', 'Tenant profile, usage, billing, users, and portal access')

@section('content')
@php
    $tenantStateLabel = ucfirst($tenant->status);
    $tenantStateClass = 'text-bg-secondary';

    if ($tenant->status === 'suspended') {
        $tenantStateLabel = 'Suspended';
    } elseif ($tenant->isExpired()) {
        $tenantStateLabel = 'Expired';
        $tenantStateClass = 'text-bg-warning';
    } elseif ($tenant->isOnTrial()) {
        $tenantStateLabel = 'Trial';
        $tenantStateClass = 'text-bg-info';
    } elseif ($tenant->isActive()) {
        $tenantStateLabel = 'Active';
        $tenantStateClass = 'text-bg-success';
    }

    $expiresAt = $tenant->subscription_ends_at ?? $tenant->trial_ends_at;
    $publicPortalUrl = route('public.tenant.show', ['tenant' => $tenant->slug]);
@endphp

<div class="row g-4">
    <div class="col-lg-4">
        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <h2 class="h5">{{ $tenant->name }}</h2>
                <div class="text-muted">{{ $tenant->email }}</div>
                <div class="mt-3">
                    <span class="badge {{ $tenantStateClass }}">{{ $tenantStateLabel }}</span>
                    @if($tenant->isOnTrial())
                        <span class="small text-muted ms-2">{{ $tenant->trialDaysRemaining() }} trial days left</span>
                    @endif
                </div>
                <hr>
                <div class="small text-muted">Owner</div>
                <div>{{ $tenant->owner_name }}</div>
                <div class="small text-muted mt-2">Phone</div>
                <div>{{ $tenant->phone ?: '-' }}</div>
                <div class="small text-muted mt-2">Plan</div>
                <div>{{ $tenant->plan?->name ?? 'None' }}</div>
                <div class="small text-muted mt-2">Assigned merchant</div>
                <div>
                    @if($tenant->agent)
                        <a href="{{ route('super.agents.show', $tenant->agent) }}">{{ $tenant->agent->displayName() }}</a>
                        <span class="text-muted small d-block">{{ ucfirst($tenant->agent_commission_type ?? 'onboarding') }} commission</span>
                    @else
                        -
                    @endif
                </div>
                <div class="small text-muted mt-2">Plan/Trial ends</div>
                <div>{{ app_datetime($expiresAt) }}</div>
                <div class="small text-muted mt-2">Registered</div>
                <div>{{ app_datetime($tenant->created_at) }}</div>
            </div>
            <div class="card-footer bg-white d-flex flex-wrap gap-2">
                @if($tenant->status !== 'suspended')
                    <form method="POST" action="{{ route('super.tenants.suspend', $tenant) }}">@csrf<button class="btn btn-warning btn-sm">Suspend</button></form>
                @else
                    <form method="POST" action="{{ route('super.tenants.reactivate', $tenant) }}">@csrf<button class="btn btn-primary btn-sm">Reactivate</button></form>
                @endif
                <form method="POST" action="{{ route('super.tenants.impersonate', $tenant) }}">
                    @csrf
                    <button class="btn btn-dark btn-sm"><i class="bi bi-box-arrow-in-right me-1"></i> Enter portal</button>
                </form>
                <button type="button" class="btn btn-outline-danger btn-sm ms-auto" data-bs-toggle="modal" data-bs-target="#deleteTenantModal">
                    <i class="bi bi-trash me-1"></i> Delete tenant
                </button>
            </div>
        </div>
    </div>
    <div class="col-lg-8">
        <div class="row g-3 mb-4">
            <div class="col-sm-6 col-xl-3">
                <div class="card border-0 shadow-sm h-100">
                    <div class="card-body">
                        <div class="text-muted small">Users</div>
                        <div class="h4 mb-0">{{ $tenant->users_count }}</div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6 col-xl-3">
                <div class="card border-0 shadow-sm h-100">
                    <div class="card-body">
                        <div class="text-muted small">Branches</div>
                        <div class="h4 mb-0">{{ $tenant->branches_count }}</div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6 col-xl-3">
                <div class="card border-0 shadow-sm h-100">
                    <div class="card-body">
                        <div class="text-muted small">Members</div>
                        <div class="h4 mb-0">{{ $tenant->customers_count }}</div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6 col-xl-3">
                <div class="card border-0 shadow-sm h-100">
                    <div class="card-body">
                        <div class="text-muted small">Loans</div>
                        <div class="h4 mb-0">{{ $tenant->loans_count }}</div>
                    </div>
                </div>
            </div>
        </div>

        <div class="card border-0 shadow-sm mb-4">
            <div class="card-header bg-white fw-semibold">Registration details</div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-6">
                        <div class="small text-muted">Company name</div>
                        <div class="fw-semibold">{{ $tenant->name }}</div>
                    </div>
                    <div class="col-md-6">
                        <div class="small text-muted">Owner name</div>
                        <div class="fw-semibold">{{ $tenant->owner_name }}</div>
                    </div>
                    <div class="col-md-6">
                        <div class="small text-muted">Company email</div>
                        <div>{{ $tenant->email }}</div>
                    </div>
                    <div class="col-md-6">
                        <div class="small text-muted">Phone number</div>
                        <div>{{ $tenant->phone ?: '-' }}</div>
                    </div>
                    <div class="col-12">
                        <div class="small text-muted">Company address</div>
                        <div>{{ $tenant->address ?: '-' }}</div>
                    </div>
                    <div class="col-md-6">
                        <div class="small text-muted">Selected plan</div>
                        <div>{{ $tenant->plan?->name ?? 'None' }}</div>
                    </div>
                    <div class="col-md-6">
                        <div class="small text-muted">Assigned merchant</div>
                        <div>
                            @if($tenant->agent)
                                <a href="{{ route('super.agents.show', $tenant->agent) }}">{{ $tenant->agent->displayName() }}</a>
                                <span class="text-muted small">since {{ app_date($tenant->agent_assigned_at, 'registration') }} · {{ ucfirst($tenant->agent_commission_type ?? 'onboarding') }} commission</span>
                            @else
                                -
                            @endif
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="small text-muted">Tenant state</div>
                        <span class="badge {{ $tenantStateClass }}">{{ $tenantStateLabel }}</span>
                    </div>
                    <div class="col-md-6">
                        <div class="small text-muted">Tenant slug</div>
                        <div>{{ $tenant->slug }}</div>
                    </div>
                    <div class="col-md-6">
                        <div class="small text-muted">Subdomain key</div>
                        <div>{{ $tenant->subdomain ?: '-' }}</div>
                    </div>
                    <div class="col-12">
                        <div class="small text-muted">Member website link</div>
                        <a href="{{ $publicPortalUrl }}" target="_blank">{{ $publicPortalUrl }}</a>
                    </div>
                    <div class="col-md-6">
                        <div class="small text-muted">Registered on</div>
                        <div>{{ app_datetime($tenant->created_at) }}</div>
                    </div>
                    <div class="col-md-6">
                        <div class="small text-muted">Auto approved on</div>
                        <div>{{ app_datetime($tenant->approved_at) }}</div>
                    </div>
                    <div class="col-md-6">
                        <div class="small text-muted">Trial ends</div>
                        <div>{{ app_datetime($tenant->trial_ends_at) }}</div>
                    </div>
                    <div class="col-md-6">
                        <div class="small text-muted">Subscription ends</div>
                        <div>{{ app_datetime($tenant->subscription_ends_at) }}</div>
                    </div>
                    <div class="col-md-6">
                        <div class="small text-muted">Currency</div>
                        <div>{{ $tenant->settings['currency'] ?? 'NGN' }}</div>
                    </div>
                    <div class="col-md-6">
                        <div class="small text-muted">Auto billing</div>
                        <div>{{ $tenant->autoBillingLabel() }}</div>
                    </div>
                </div>
            </div>
        </div>

        <div class="card border-0 shadow-sm mb-4">
            <div class="card-header bg-white fw-semibold">Users</div>
            <div class="table-responsive">
                <table class="table mb-0"><thead><tr><th>Name</th><th>Email</th><th>Role</th><th>Status</th></tr></thead><tbody>
                    @foreach($tenant->users as $user)
                        <tr><td>{{ $user->name }}</td><td>{{ $user->email }}</td><td>{{ $user->roleLabel() }}</td><td>{{ $user->status }}</td></tr>
                    @endforeach
                </tbody></table>
            </div>
        </div>
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white fw-semibold">Payments</div>
            <div class="table-responsive">
                <table class="table mb-0"><thead><tr><th>Reference</th><th>Plan</th><th>Amount</th><th>Status</th></tr></thead><tbody>
                    @foreach($tenant->payments as $payment)
                        <tr><td>{{ $payment->reference }}</td><td>{{ $payment->plan?->name }}</td><td>{{ number_format($payment->amount, 2) }}</td><td>{{ $payment->status }}</td></tr>
                    @endforeach
                </tbody></table>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="deleteTenantModal" tabindex="-1" aria-labelledby="deleteTenantModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow">
            <form method="POST" action="{{ route('super.tenants.destroy', $tenant) }}">
                @csrf
                @method('DELETE')
                <div class="modal-header border-0 pb-0">
                    <div>
                        <div class="text-danger small fw-semibold text-uppercase">Permanent delete</div>
                        <h5 class="modal-title" id="deleteTenantModalLabel">Delete {{ $tenant->name }}?</h5>
                    </div>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="alert alert-danger mb-3">
                        This will permanently delete this tenant, its users, members, branches, products, savings, loans, investments, reports, messages, and settings. This action cannot be undone.
                    </div>
                    <label for="confirm_name" class="form-label">Type the company name to confirm</label>
                    <input
                        type="text"
                        name="confirm_name"
                        id="confirm_name"
                        value="{{ old('confirm_name') }}"
                        class="form-control @error('confirm_name') is-invalid @enderror"
                        placeholder="{{ $tenant->name }}"
                        autocomplete="off"
                        required
                    >
                    @error('confirm_name')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                    <div class="form-text">Required text: <span class="fw-semibold">{{ $tenant->name }}</span></div>
                </div>
                <div class="modal-footer border-0">
                    <button type="button" class="btn btn-light" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-danger">Permanently delete tenant</button>
                </div>
            </form>
        </div>
    </div>
</div>

@if($errors->has('confirm_name'))
    @push('scripts')
        <script>
            document.addEventListener('DOMContentLoaded', () => {
                const modal = document.getElementById('deleteTenantModal');
                if (modal && window.bootstrap) {
                    window.bootstrap.Modal.getOrCreateInstance(modal).show();
                }
            });
        </script>
    @endpush
@endif
@endsection
