<?php

namespace App\Http\Controllers\Dashboard;

use App\Enums\OrganizerPrimaryEventType;
use App\Http\Controllers\Controller;
use App\Http\Requests\UpdateOrganizerProfileRequest;
use App\Models\Organizer;
use App\Models\Role;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

/**
 * Perfil extendido del organizador: datos de contacto, tipo de eventos, cuentas PayPal/Stripe según negocio.
 */
class OrganizerProfileController extends Controller
{
    public function edit(): View
    {
        $user = auth()->user();
        abort_unless($user->hasRole(Role::SLUG_ORGANIZADOR), 403);

        $user->load('organizer');
        if ($user->organizer === null) {
            Organizer::query()->create([
                'user_id' => $user->id,
                'primary_event_type' => OrganizerPrimaryEventType::Other,
            ]);
            $user->load('organizer');
        } elseif ($user->organizer->primary_event_type === null) {
            $user->organizer->update(['primary_event_type' => OrganizerPrimaryEventType::Other]);
            $user->load('organizer');
        }

        return view('dashboard.profile.organizer', ['user' => $user]);
    }

    public function update(UpdateOrganizerProfileRequest $request): RedirectResponse
    {
        $user = $request->user();
        $data = $request->validated();

        $user->forceFill([
            'name' => $data['name'],
            'phone' => $data['phone'],
            'organization_name' => $data['organization_name'],
        ])->save();

        $organizer = $user->organizer ?? Organizer::query()->create([
            'user_id' => $user->id,
            'primary_event_type' => $data['primary_event_type'],
        ]);
        $organizer->forceFill([
            'paypal_email' => $data['paypal_email'] ?? null,
            'primary_event_type' => $data['primary_event_type'],
        ])->save();

        return redirect()->route('dashboard.profile')->with('success', __('Perfil actualizado.'));
    }
}
