<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use App\Http\Middleware\TenantMiddleware;

class FirstLoginController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
        app(TenantMiddleware::class)->setupTenantConnection();
    }

    public function show()
    {
        $user = Auth::user();

        // If already completed first login, redirect to dashboard
        if ($user->first_login_completed) {
            return redirect('/dashboard');
        }

        return view('first-login');
    }

    public function complete(Request $request)
    {
        // Validate only password
        $request->validate([
            'password' => 'required|min:6|confirmed',
        ], [
            'password.required' => 'La password è obbligatoria',
            'password.min' => 'La password deve essere di almeno 6 caratteri',
            'password.confirmed' => 'Le password non coincidono',
        ]);

        $currentUser = Auth::user();

        Log::info('Starting first login completion', [
            'user_id' => $currentUser->id,
            'email' => $currentUser->email
        ]);

        try {
            $this->updateTenantDatabase($currentUser, $request->password);

            $this->updateMasterDatabaseSafely($currentUser, $request->password);

            $this->sendAccountActivationEmailSafely($currentUser);

            return redirect('/dashboard')->with('success', 'Password impostata con successo! Benvenuto in Leezard.cloud.');

        } catch (\Exception $e) {
            Log::error('First login completion failed', [
                'user_id' => $currentUser->id,
                'error' => $e->getMessage(),
                'trace' => $e->getTraceAsString()
            ]);

            return back()
                ->withErrors(['error' => 'Errore durante l\'impostazione della password. Riprova.']);
        }
    }

    private function updateTenantDatabase($currentUser, $password)
    {
        try {
            Log::info('Updating tenant database', ['user_id' => $currentUser->id]);

            $user = \App\Models\User::findOrFail($currentUser->id);
            $user->password = Hash::make($password);
            $user->first_login_completed = true;
            $user->email_verified_at = now();
            $user->save();

            Log::info('Tenant database updated successfully', ['user_id' => $currentUser->id]);

        } catch (\Exception $e) {
            Log::error('Failed to update tenant database', [
                'user_id' => $currentUser->id,
                'error' => $e->getMessage()
            ]);
            throw new \Exception('Errore durante l\'aggiornamento del database locale: ' . $e->getMessage());
        }
    }

    private function updateMasterDatabaseSafely($currentUser, $password)
    {
        try {
            Log::info('Attempting to update master database', ['user_id' => $currentUser->id]);

            if (!env('DB_DATABASE')) {
                Log::warning('No master database configured, skipping master update');
                return;
            }

            $this->updateMasterDatabaseWithTimeout($currentUser, $password);

        } catch (\Exception $e) {
            Log::error('Master database update failed (non-critical)', [
                'user_id' => $currentUser->id,
                'error' => $e->getMessage()
            ]);
        }
    }

    private function updateMasterDatabaseWithTimeout($currentUser, $password)
    {
        $originalTimeout = ini_get('max_execution_time');
        set_time_limit(10);
        try {
            $masterConfig = [
                'driver' => 'mysql',
                'host' => env('DB_HOST', '127.0.0.1'),
                'port' => env('DB_PORT', '3306'),
                'database' => env('DB_DATABASE'),
                'username' => env('DB_USERNAME'),
                'password' => env('DB_PASSWORD'),
                'charset' => 'utf8mb4',
                'collation' => 'utf8mb4_unicode_ci',
                'prefix' => '',
                'strict' => true,
                'engine' => null,
                'options' => [
                    \PDO::ATTR_TIMEOUT => 5,
                ],
            ];

            config(['database.connections.master_temp' => $masterConfig]);

            $connection = DB::connection('master_temp');
            $connection->getPdo();

            Log::info('Master database connection established');

            $updateData = [
                'password' => Hash::make($password),
                'first_login_completed' => true,
                'email_verified_at' => now(),
                'updated_at' => now()
            ];

            $updated = $connection->table('users')
                ->where('email', $currentUser->email)
                ->update($updateData);

            if ($updated) {
                Log::info('Master database updated successfully', [
                    'email' => $currentUser->email,
                    'rows_updated' => $updated
                ]);
            } else {
                Log::warning('No rows updated in master database', [
                    'email' => $currentUser->email
                ]);
            }

            DB::purge('master_temp');

        } catch (\Exception $e) {
            Log::error('Master database update timeout or error', [
                'error' => $e->getMessage(),
                'user_email' => $currentUser->email
            ]);

            try {
                DB::purge('master_temp');
            } catch (\Exception $cleanupError) {
                // Ignore cleanup errors
            }

            throw $e;
        } finally {
            set_time_limit($originalTimeout);
        }
    }

    private function sendAccountActivationEmailSafely($user)
    {
        try {
            Log::info('Attempting to send activation email', ['user_id' => $user->id]);

            $emailData = [
                'name' => $user->name,
                'email' => $user->email,
                'login_url' => url('/'),
                'activation_time' => now()->format('d/m/Y H:i'),
                'platform_name' => 'Leezard.cloud'
            ];

            $originalTimeout = ini_get('max_execution_time');
            set_time_limit(15);

            Mail::send('emails.account-activated', $emailData, function ($message) use ($user) {
                $message->to($user->email, $user->name)
                        ->subject('Il tuo account è attivo – Benvenuto in Leezard.cloud')
                        ->from(config('mail.from.address'), config('mail.from.name'));
            });

            set_time_limit($originalTimeout);

            Log::info('Activation email sent successfully', [
                'user_id' => $user->id,
                'email' => $user->email
            ]);

        } catch (\Exception $e) {
            Log::error('Failed to send activation email (non-critical)', [
                'user_id' => $user->id,
                'email' => $user->email,
                'error' => $e->getMessage()
            ]);
        }
    }
}
