<?php

namespace App\Http\Controllers\Agency;

use App\Http\Controllers\Controller;
use App\Models\Customer\Customer;
use App\Models\CustomerPassenger;
use Illuminate\Http\Request;

class CustomerPassengerController extends Controller
{
    public function store(Request $request, Customer $customer)
    {
        $validated = $request->validate([
            'first_name' => 'required|string|max:255',
            'last_name' => 'required|string|max:255',
            'gender' => 'nullable|in:male,female,other',
            'birth_date' => 'nullable|date',
            'national_id' => 'nullable|string|max:20',
            'passport_number' => 'nullable|string|max:20',
            'passport_expiry' => 'nullable|date',
            'visa_status' => 'nullable|string|max:255',
            'visa_expiry' => 'nullable|date',
            'relationship' => 'nullable|string|max:255',
            'phone' => 'nullable|string|max:20',
            'email' => 'nullable|email|max:255',
        ]);

        $validated['created_by'] = auth()->id();

        $customer->passengers()->create($validated);

        return back()->with('success', 'Yolcu başarıyla eklendi.');
    }

    public function update(Request $request, Customer $customer, CustomerPassenger $passenger)
    {
        // Yetki kontrolü (passenger'ın bu customer'a ait olduğundan emin olmak)
        if ($passenger->customer_id !== $customer->id) {
            abort(403);
        }

        $validated = $request->validate([
            'first_name' => 'required|string|max:255',
            'last_name' => 'required|string|max:255',
            'gender' => 'nullable|in:male,female,other',
            'birth_date' => 'nullable|date',
            'national_id' => 'nullable|string|max:20',
            'passport_number' => 'nullable|string|max:20',
            'passport_expiry' => 'nullable|date',
            'visa_status' => 'nullable|string|max:255',
            'visa_expiry' => 'nullable|date',
            'relationship' => 'nullable|string|max:255',
            'phone' => 'nullable|string|max:20',
            'email' => 'nullable|email|max:255',
        ]);

        $passenger->update($validated);

        return back()->with('success', 'Yolcu başarıyla güncellendi.');
    }

    public function destroy(Customer $customer, CustomerPassenger $passenger)
    {
        if ($passenger->customer_id !== $customer->id) {
            abort(403);
        }

        $passenger->delete();

        return back()->with('success', 'Yolcu başarıyla silindi.');
    }

    /** Hızlı düzenleme (AJAX) */
    public function updateField(Request $request, Customer $customer, CustomerPassenger $passenger)
    {
        if ($passenger->customer_id !== $customer->id) abort(403);

        $request->validate([
            'field' => 'required|string',
            'value' => 'nullable',
        ]);

        $field = $request->field;
        $value = $request->value;

        $allowedFields = [
            'first_name', 'last_name', 'gender', 'birth_date', 
            'national_id', 'passport_number', 'passport_expiry', 
            'visa_status', 'visa_expiry', 'relationship', 'phone', 'email'
        ];

        if (!in_array($field, $allowedFields)) {
            return response()->json(['success' => false, 'message' => 'Geçersiz alan.'], 422);
        }

        try {
            $passenger->update([$field => $value]);
            
            $displayValue = $value;
            if ($field === 'birth_date' || $field === 'passport_expiry' || $field === 'visa_expiry') {
                $displayValue = $value ? \Carbon\Carbon::parse($value)->format('d.m.Y') : '—';
            }
            if ($field === 'gender') {
                $displayValue = $value ? ['male'=>'Erkek','female'=>'Kadın','other'=>'Diğer'][$value] : '—';
            }

            return response()->json([
                'success' => true,
                'message' => 'Güncellendi.',
                'value' => $value,
                'display_value' => $displayValue
            ]);
        } catch (\Exception $e) {
            return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
        }
    }
}
