<?php

namespace App\Http\Controllers;

use App\Models\TransactionModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\Response;

class TransactionController extends Controller
{
    public function index(Request $request)
    {
        $query = TransactionModel::with(['ticket', 'ticket.match', 'user']);

        if ($search = $request->input('search')) {
            $query->whereHas('ticket', function ($q) use ($search) {
                $q->where('ticket_number', 'LIKE', "%$search%")
                    ->orWhereHas('match', function ($mq) use ($search) {
                        $mq->where('home_team', 'LIKE', "%$search%")
                            ->orWhere('away_team', 'LIKE', "%$search%");
                    });
            });
        }
        if ($user = $request->input('user')) {
            $query->whereHas('user', function ($q) use ($user) {
                $q->where('name', 'LIKE', "%$user%");
            });
        }
        if ($method = $request->input('payment_method')) {
            $query->where('payment_method', $method);
        }
        if ($from = $request->input('from')) {
            $query->whereDate('created_at', ">=", $from);
        }
        if ($to = $request->input('to')) {
            $query->whereDate('created_at', "<=", $to);
        }
        $transactions = $query->orderBy('created_at', 'desc')->paginate(20)->withQueryString();
        return view('admin.transactions.index', compact('transactions'));
    }

    // CSV export stub
    public function exportCsv(Request $request)
    {
        // Filtering logic can be reused
        $query = TransactionModel::with(['ticket', 'ticket.match', 'user']);
        // filter logic (same as above)
        // ... implementation in next steps ...
        return response('CSV export not implemented yet', Response::HTTP_NOT_IMPLEMENTED);
    }

    // PDF export stub
    public function exportPdf(Request $request)
    {
        // Filtering logic can be reused
        $query = TransactionModel::with(['ticket', 'ticket.match', 'user']);
        // ... implementation in next steps ...
        return response('PDF export not implemented yet', Response::HTTP_NOT_IMPLEMENTED);
    }
}
