<?php

namespace App\Http\Controllers;

use App\Models\Event;
use App\Models\KategoriEvent;
use App\Models\Lokasi;
use App\Models\Transaksi;

class UserHomeController extends Controller
{
    public function index()
    {
        $events = Event::with([
            'kategori',
            'lokasi',
            'tikets'
        ])->latest()->take(6)->get();

        $kategori = KategoriEvent::all();
        $lokasi = Lokasi::all();

        return view('user.home', compact('events', 'kategori', 'lokasi'));
    }

    public function event(
        \Illuminate\Http\Request $request
    )
    {
        $perPage = (int) $request->query('perpage', 9);

        $query = Event::with(['kategori', 'lokasi', 'tikets'])->withMin('tikets', 'harga');

        if ($search = $request->query('search')) {
            $query->where(function ($q) use ($search) {
                $q->where('nama_event', 'like', "%{$search}%")
                    ->orWhere('deskripsi', 'like', "%{$search}%")
                    ->orWhereHas('kategori', function ($q2) use ($search) {
                        $q2->where('nama_kategori', 'like', "%{$search}%");
                    })
                    ->orWhereHas('lokasi', function ($q2) use ($search) {
                        $q2->where('nama_lokasi', 'like', "%{$search}%");
                    });
            });
        }

        if ($kategori = $request->query('kategori')) {
            $query->where('kategori_id', $kategori);
        }

        if ($lokasi = $request->query('lokasi')) {
            $query->where('lokasi_id', $lokasi);
        }

        // date filter
        if ($date = $request->query('date_filter')) {
            if ($date === 'today') {
                $query->whereDate('tanggal', now()->toDateString());
            } elseif ($date === 'this_week') {
                $query->whereBetween('tanggal', [now()->startOfWeek(), now()->endOfWeek()]);
            } elseif ($date === 'this_month') {
                $query->whereYear('tanggal', now()->year)->whereMonth('tanggal', now()->month);
            } elseif ($date === 'custom') {
                $from = $request->query('date_from');
                $to = $request->query('date_to');
                if ($from) $query->whereDate('tanggal', '>=', $from);
                if ($to) $query->whereDate('tanggal', '<=', $to);
            }
        }

        // sorting
        switch ($request->query('sort')) {
            case 'oldest':
                $query->orderBy('tanggal', 'asc');
                break;
            case 'price_asc':
                $query->orderBy('tikets_min_harga', 'asc');
                break;
            case 'price_desc':
                $query->orderBy('tikets_min_harga', 'desc');
                break;
            case 'name_asc':
                $query->orderBy('nama_event', 'asc');
                break;
            case 'name_desc':
                $query->orderBy('nama_event', 'desc');
                break;
            default:
                $query->orderBy('tanggal', 'desc');
                break;
        }

        $events = $query->paginate($perPage)->appends($request->except('page'));

        $kategori = KategoriEvent::all();
        $lokasi = Lokasi::all();

        return view('user.event', compact('events', 'kategori', 'lokasi'));
    }

    public function riwayat()
    {
        $transaksis = Transaksi::with([
            'tiket.event'
        ])
        ->where('user_id', auth()->id())
        ->latest()
        ->paginate(10);

        return view('user.riwayat', compact('transaksis'));
    }
}