<?php

namespace App\Http\Controllers\Agency\DayTour;

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

class DayTourItineraryController extends Controller
{
    use AuthorizesRequests;

    public function index(DayTour $tour)
    {
        $itineraries = $tour->itineraries()
            ->orderBy('sort_order')
            ->get();

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

    public function store(Request $request, DayTour $tour)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'location' => 'nullable|string|max:255',
            'time' => 'nullable|date_format:H:i',
            'duration_minutes' => 'nullable|integer|min:0',
            'activity_type' => 'nullable|string|max:255',
            'sort_order' => 'nullable|integer|min:0',
        ]);

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

        // Calculate sort_order if not provided
        if (!isset($data['sort_order'])) {
            $data['sort_order'] = DayTourItinerary::where('day_tour_id', $tour->id)->max('sort_order') + 1;
        }

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

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

    public function edit(DayTour $tour, DayTourItinerary $itinerary)
    {
        return response()->json([
            'success' => true,
            'itinerary' => $itinerary
        ]);
    }

    public function update(Request $request, DayTour $tour, DayTourItinerary $itinerary)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'location' => 'nullable|string|max:255',
            'time' => 'nullable|date_format:H:i',
            'duration_minutes' => 'nullable|integer|min:0',
            'activity_type' => 'nullable|string|max:255',
        ]);

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

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

    public function destroy(DayTour $tour, DayTourItinerary $itinerary)
    {
        $itinerary->delete();

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

    public function reorder(Request $request, DayTour $tour)
    {
        $request->validate([
            'items' => 'required|array',
            'items.*.id' => 'required|exists:day_tour_itineraries,id',
            'items.*.sort_order' => 'required|integer|min:0',
        ]);

        DB::beginTransaction();
        try {
            foreach ($request->items as $item) {
                DayTourItinerary::where('id', $item['id'])
                    ->where('day_tour_id', $tour->id)
                    ->update([
                        '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);
        }
    }
}
