@extends('saas-admin.layout')

@section('title', 'Companies')

@section('content')
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h4 class="m-0">{{ __('Companies') }}</h4>
        <a href="{{ route('saas-admin.companies.create') }}" class="btn btn-primary btn-sm">
            {{ __('+ New company') }}
        </a>
    </div>

    <div class="card shadow-sm">
        <div class="table-responsive">
            <table class="table table-hover align-middle m-0">
                <thead class="table-light">
                    <tr>
                        <th>{{ __('Company') }}</th>
                        <th>{{ __('Domain') }}</th>
                        <th>{{ __('Admin') }}</th>
                        <th>{{ __('Status') }}</th>
                        <th>{{ __('Created') }}</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($companies as $company)
                        @php $primaryDomain = $company->domains->firstWhere('is_primary', true); @endphp
                        <tr>
                            <td>{{ $company->name }}</td>
                            <td>
                                @if ($primaryDomain)
                                    <a href="https://{{ $primaryDomain->hostname }}" target="_blank" rel="noopener">
                                        {{ $primaryDomain->hostname }}
                                    </a>
                                @else
                                    <span class="text-muted">—</span>
                                @endif
                            </td>
                            <td>
                                @if ($company->owner)
                                    {{ $company->owner->name }}<br>
                                    <span class="text-muted small">{{ $company->owner->email }}</span>
                                @else
                                    <span class="text-muted">—</span>
                                @endif
                            </td>
                            <td>
                                <span class="badge {{ $company->statusBadgeClass() }}">{{ ucfirst($company->status) }}</span>
                            </td>
                            <td>{{ $company->created_at->format('M j, Y') }}</td>
                            <td class="text-end">
                                <a href="{{ route('saas-admin.companies.show', $company) }}" class="btn btn-sm btn-outline-secondary">
                                    {{ __('Manage') }}
                                </a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="text-center text-muted py-4">
                                {{ __('No companies yet — create the first one.') }}
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    <div class="mt-3">
        {{ $companies->links() }}
    </div>
@endsection
