<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Mail\WelcomeEmail;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;

class EmailTestController extends Controller
{
    /**
     * Enviar email de teste com boas práticas anti-spam
     * 
     * Este endpoint demonstra:
     * - Headers corretos (From, Reply-To, List-Unsubscribe)
     * - DKIM (configurado no servidor Postfix)
     * - SPF (configurado no DNS)
     * - HTML + Plain Text versions
     * - Conteúdo bem formatado e sem palavras suspeitas
     */
    public function sendTest(Request $request): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'to_email' => 'required|email',
                'to_name' => 'required|string|max:255',
            ], [
                'to_email.required' => 'El email destino es obligatorio',
                'to_email.email' => 'El email debe ser válido',
                'to_name.required' => 'El nombre es obligatorio',
            ]);

            if ($validator->fails()) {
                return response()->json([
                    'success' => false,
                    'message' => 'Error de validación',
                    'errors' => $validator->errors()
                ], 422);
            }

            // Enviar email de bienvenida
            Mail::to($request->to_email)
                ->send(new WelcomeEmail($request->to_name, $request->to_email));

            return response()->json([
                'success' => true,
                'message' => 'Email enviado exitosamente',
                'data' => [
                    'to' => $request->to_email,
                    'subject' => '¡Bienvenido a WEBMoney! Tu cuenta ha sido creada',
                    'from' => 'no-reply@cnxifly.com',
                    'reply_to' => 'support@cnxifly.com',
                    'features' => [
                        'DKIM signature' => 'Enabled (OpenDKIM)',
                        'SPF record' => 'Configured in DNS',
                        'HTML + Text' => 'Both versions included',
                        'Reply-To' => 'Configured to support@cnxifly.com',
                        'Professional design' => 'Mobile-responsive template',
                    ]
                ]
            ], 200);

        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Error al enviar email',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    /**
     * Obter informações sobre configuração anti-spam
     */
    public function getAntiSpamInfo(): JsonResponse
    {
        return response()->json([
            'success' => true,
            'data' => [
                'server' => [
                    'mail_server' => 'mail.cnxifly.com',
                    'smtp_port' => 587,
                    'encryption' => 'TLS',
                    'from_address' => 'no-reply@cnxifly.com',
                    'reply_to' => 'support@cnxifly.com',
                ],
                'anti_spam_features' => [
                    'DKIM' => [
                        'status' => 'Enabled',
                        'selector' => 'default',
                        'algorithm' => 'RSA-SHA256',
                        'key_size' => '2048 bits',
                        'dns_record' => 'default._domainkey.cnxifly.com',
                    ],
                    'SPF' => [
                        'status' => 'Configured',
                        'policy' => 'v=spf1 mx a ip4:213.165.93.60 ~all',
                        'recommendation' => 'Consider changing ~all to -all for stricter validation',
                    ],
                    'DMARC' => [
                        'status' => 'Not configured yet',
                        'recommendation' => 'Configure DMARC record for better deliverability',
                    ],
                ],
                'email_best_practices' => [
                    'HTML + Plain Text' => 'Both versions provided',
                    'Responsive Design' => 'Mobile-friendly template',
                    'Unsubscribe Link' => 'To be implemented for marketing emails',
                    'Professional From Name' => 'WEBMoney - ConneXiFly',
                    'Reply-To Address' => 'Separate support email configured',
                    'No Spam Words' => 'Content reviewed for spam triggers',
                ],
                'testing' => [
                    'test_email' => 'marcoitaloesp@gmail.com',
                    'reason' => 'Gmail has strictest spam filters - if it passes Gmail, it will pass most other providers',
                ],
            ]
        ], 200);
    }
}
