<?php

namespace App\Http\Controllers\Agency\InternationalTour;

use App\Http\Controllers\Controller;
use App\Models\InternationalTour\InternationalTour;
use App\Models\InternationalTour\InternationalTourItinerary;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class InternationalTourItineraryController extends Controller
{
    use AuthorizesRequests;

    public function index(InternationalTour $yurtdisiTur)
    {
        $tour = $yurtdisiTur;
        $this->authorize('update', $tour);

        $itineraries = $tour->itineraries()
            ->ordered()
            ->get()
            ->groupBy('day_number');

        return view('agency.products.yurtdisi-turlari.itinerary', compact('tour', 'itineraries'));
    }

    public function store(Request $request, InternationalTour $yurtdisiTur)
    {
        $tour = $yurtdisiTur;
        $this->authorize('update', $tour);

        $request->validate([
            'day_number' => 'required|integer|min:1',
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'location' => 'nullable|string|max:255',
            'sort_order' => 'nullable|integer|min:0',
        ]);

        $data = $request->all();
        $data['international_tour_id'] = $tour->id;

        if (!isset($data['sort_order'])) {
            $data['sort_order'] = InternationalTourItinerary::where('international_tour_id', $tour->id)
                    ->where('day_number', $request->day_number)
                    ->max('sort_order') + 1;
        }

        $itinerary = InternationalTourItinerary::create($data);

        return response()->json([
            'success' => true,
            'itinerary' => $itinerary,
            'message' => 'Program eklendi.'
        ]);
    }

    public function edit(InternationalTour $yurtdisiTur, InternationalTourItinerary $itinerary)
    {
        $tour = $yurtdisiTur;
        $this->authorize('update', $tour);

        return response()->json([
            'success' => true,
            'itinerary' => $itinerary
        ]);
    }

    public function update(Request $request, InternationalTour $yurtdisiTur, InternationalTourItinerary $itinerary)
    {
        $tour = $yurtdisiTur;
        $this->authorize('update', $tour);

        $request->validate([
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'location' => 'nullable|string|max:255',
        ]);

        $itinerary->update($request->all());

        return response()->json([
            'success' => true,
            'message' => 'Program güncellendi.'
        ]);
    }

    public function destroy(InternationalTour $yurtdisiTur, InternationalTourItinerary $itinerary)
    {
        $tour = $yurtdisiTur;
        $this->authorize('update', $tour);

        $itinerary->delete();

        return response()->json([
            'success' => true,
            'message' => 'Program silindi.'
        ]);
    }

    public function reorder(Request $request, InternationalTour $yurtdisiTur)
    {
        $tour = $yurtdisiTur;
        $this->authorize('update', $tour);

        $request->validate([
            'items' => 'required|array',
            'items.*.id' => 'required|exists:international_tour_itineraries,id',
            'items.*.day_number' => 'required|integer|min:1',
            'items.*.sort_order' => 'required|integer|min:0',
        ]);

        DB::beginTransaction();
        try {
            foreach ($request->items as $item) {
                InternationalTourItinerary::where('id', $item['id'])
                    ->where('international_tour_id', $tour->id)
                    ->update([
                        'day_number' => $item['day_number'],
                        'sort_order' => $item['sort_order']
                    ]);
            }

            DB::commit();

            return response()->json([
                'success' => true,
                'message' => 'Program sıralaması güncellendi.'
            ]);

        } catch (\Exception $e) {
            DB::rollback();
            return response()->json([
                'success' => false,
                'message' => 'Sıralama güncellenirken hata oluştu.'
            ], 422);
        }
    }
}
