@extends('layouts.admin.theme')
@section('title', env('APP_NAME') . ' :: ' . __('Diagnóstico de E-mail'))
@section('content')

    @include('errors.messageFlash')

    <div class="card shadow-sm">

        @include('components.buttons._headLineButtonsBackAdd', [
            'headLine' => __('Diagnóstico de E-mail'),
            'routeBack' => 'admin.dashboard',
            'routeBackParams' => [],
            'nameBack' => __('Voltar'),
            'permission' => 'SYSADMIN',
        ])

        <div class="card-body">

            <div class="row g-4">

                {{-- Formulário de envio de teste --}}
                <div class="col-lg-6">
                    <div class="card h-100 border-0 bg-light">
                        <div class="card-body">
                            <h5 class="card-title mb-3">
                                <i class="fa-solid fa-paper-plane me-2"></i>{{ __('Enviar e-mail de teste') }}
                            </h5>

                            @if (auth()->user()->getPermissaoDetalhes('SYSADMIN')['adicionar'])
                                <form action="{{ route('admin.diagnostico.email.enviar') }}" method="post">
                                    @csrf

                                    <div class="mb-3">
                                        <label for="destinatario" class="form-label">{{ __('E-mail de destino') }}</label>
                                        <input type="email" class="form-control" id="destinatario" name="destinatario"
                                            value="{{ old('destinatario') }}"
                                            placeholder="{{ __('exemplo@dominio.com') }}" required>
                                    </div>

                                    <div class="mb-3">
                                        <label for="mailer" class="form-label">{{ __('Mailer') }}</label>
                                        <select class="form-select" id="mailer" name="mailer" required>
                                            @foreach ($mailers as $mailerOption)
                                                <option value="{{ $mailerOption }}"
                                                    @selected(old('mailer', $config['default']) === $mailerOption)>
                                                    {{ $mailerOption }}
                                                </option>
                                            @endforeach
                                        </select>
                                        <div class="form-text">
                                            {{ __('Use "smtp" para testar o servidor real e "log" para validar a aplicação sem enviar.') }}
                                        </div>
                                    </div>

                                    <div class="mb-3">
                                        <label for="observacao" class="form-label">{{ __('Observação (opcional)') }}</label>
                                        <textarea class="form-control" id="observacao" name="observacao" rows="2"
                                            maxlength="500"
                                            placeholder="{{ __('Texto livre incluído no corpo do e-mail de teste.') }}">{{ old('observacao') }}</textarea>
                                    </div>

                                    <button type="submit" class="btn btn-primary">
                                        <i class="fa-solid fa-paper-plane me-1"></i>{{ __('Enviar teste') }}
                                    </button>
                                </form>
                            @else
                                <p class="text-muted fst-italic mb-0">
                                    {{ __('Você não tem permissão para enviar e-mails de teste.') }}
                                </p>
                            @endif
                        </div>
                    </div>
                </div>

                {{-- Configuração atual (sem expor a senha) --}}
                <div class="col-lg-6">
                    <div class="card h-100 border-0 bg-light">
                        <div class="card-body">
                            <h5 class="card-title mb-3">
                                <i class="fa-solid fa-gears me-2"></i>{{ __('Configuração atual') }}
                            </h5>
                            <ul class="list-group list-group-flush bg-transparent">
                                <li class="list-group-item bg-transparent d-flex justify-content-between">
                                    <span class="text-muted">{{ __('Mailer padrão') }}</span>
                                    <strong>{{ $config['default'] ?? '-' }}</strong>
                                </li>
                                <li class="list-group-item bg-transparent d-flex justify-content-between">
                                    <span class="text-muted">{{ __('Transporte') }}</span>
                                    <strong>{{ $config['transport'] ?? '-' }}</strong>
                                </li>
                                <li class="list-group-item bg-transparent d-flex justify-content-between">
                                    <span class="text-muted">{{ __('Host') }}</span>
                                    <strong>{{ $config['host'] ?? '-' }}</strong>
                                </li>
                                <li class="list-group-item bg-transparent d-flex justify-content-between">
                                    <span class="text-muted">{{ __('Porta') }}</span>
                                    <strong>{{ $config['port'] ?? '-' }}</strong>
                                </li>
                                <li class="list-group-item bg-transparent d-flex justify-content-between">
                                    <span class="text-muted">{{ __('Criptografia') }}</span>
                                    <strong>{{ $config['encryption'] ?? __('(nenhuma)') }}</strong>
                                </li>
                                <li class="list-group-item bg-transparent d-flex justify-content-between">
                                    <span class="text-muted">{{ __('Usuário') }}</span>
                                    <strong>{{ $config['username'] ?? '-' }}</strong>
                                </li>
                                <li class="list-group-item bg-transparent d-flex justify-content-between">
                                    <span class="text-muted">{{ __('Remetente') }}</span>
                                    <strong>{{ $config['from_address'] ?? '-' }}</strong>
                                </li>
                            </ul>
                            <p class="form-text mt-2 mb-0">
                                <i class="fa-solid fa-shield-halved me-1"></i>{{ __('A senha de e-mail nunca é exibida por segurança.') }}
                            </p>
                        </div>
                    </div>
                </div>
            </div>

            {{-- Histórico de tentativas --}}
            <h5 class="mt-4 mb-3">
                <i class="fa-solid fa-clock-rotate-left me-2"></i>{{ __('Histórico de testes') }}
            </h5>

            <div class="table-responsive">
                <table class="table table-bordered table-hover table-striped align-middle text-center">
                    <thead class="table-light">
                        <tr>
                            <th>{{ __('Data') }}</th>
                            <th>{{ __('Destinatário') }}</th>
                            <th>{{ __('Mailer') }}</th>
                            <th>{{ __('Resultado') }}</th>
                            <th>{{ __('Código') }}</th>
                            <th>{{ __('Tempo') }}</th>
                            <th>{{ __('Solicitante') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($logs as $log)
                            <tr>
                                <td>{{ $log->created_at?->format('d/m/Y H:i:s') }}</td>
                                <td>{{ $log->recipient }}</td>
                                <td>{{ $log->mailer }}</td>
                                <td>
                                    @if ($log->success)
                                        <span class="badge bg-success">{{ __('Sucesso') }}</span>
                                    @else
                                        <span class="badge bg-danger" title="{{ $log->result_message }}">{{ __('Falha') }}</span>
                                    @endif
                                </td>
                                <td>{{ $log->smtp_code ?? '-' }}</td>
                                <td>{{ $log->duration_ms !== null ? $log->duration_ms . ' ms' : '-' }}</td>
                                <td>{{ $log->user_name ?? ($log->user->name ?? '-') }}</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="text-muted fst-italic">{{ __('* Nenhum teste registrado ainda.') }}</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <div class="d-flex justify-content-center mt-4">
                {!! $logs->links() !!}
            </div>
        </div>
    </div>
@endsection
