<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\AdminExpense;
use Illuminate\Http\Request;
use Carbon\Carbon;

class AdminExpenseController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Expense List
    |--------------------------------------------------------------------------
    */
    public function index(Request $request)
    {
        $query = AdminExpense::query();

        // Search by title or note
        if ($request->filled('search')) {
            $search = $request->search;

            $query->where(function ($q) use ($search) {
                $q->where('title', 'like', '%' . $search . '%')
                  ->orWhere('note', 'like', '%' . $search . '%');
            });
        }

        // Date filters
        if ($request->filled('from_date')) {
            $query->whereDate('created_at', '>=', $request->from_date);
        }

        if ($request->filled('to_date')) {
            $query->whereDate('created_at', '<=', $request->to_date);
        }

        $expenses = $query->latest()->paginate(10)->appends($request->query());

        // Summary Cards
        $totalExpense = AdminExpense::sum('amount');

        $thisMonthExpense = AdminExpense::whereYear('created_at', Carbon::now()->year)
            ->whereMonth('created_at', Carbon::now()->month)
            ->sum('amount');

        $todayExpense = AdminExpense::whereDate('created_at', Carbon::today())
            ->sum('amount');

        return view('admin.expenses.index', compact(
            'expenses',
            'totalExpense',
            'thisMonthExpense',
            'todayExpense'
        ));
    }

    /*
    |--------------------------------------------------------------------------
    | Create Expense
    |--------------------------------------------------------------------------
    */
    public function create()
    {
        return view('admin.expenses.create');
    }

    /*
    |--------------------------------------------------------------------------
    | Store Expense
    |--------------------------------------------------------------------------
    */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'title'  => ['required', 'string', 'max:255'],
            'amount' => ['required', 'numeric', 'min:0'],
            'note'   => ['nullable', 'string'],
        ]);

        AdminExpense::create($validated);

        return redirect()
            ->route('admin-expenses.index')
            ->with('success', 'Expense added successfully');
    }

    /*
    |--------------------------------------------------------------------------
    | Edit Expense
    |--------------------------------------------------------------------------
    */
    public function edit(AdminExpense $admin_expense)
    {
        return view('admin.expenses.edit', compact('admin_expense'));
    }

    /*
    |--------------------------------------------------------------------------
    | Update Expense
    |--------------------------------------------------------------------------
    */
    public function update(Request $request, AdminExpense $admin_expense)
    {
        $validated = $request->validate([
            'title'  => ['required', 'string', 'max:255'],
            'amount' => ['required', 'numeric', 'min:0'],
            'note'   => ['nullable', 'string'],
        ]);

        $admin_expense->update($validated);

        return redirect()
            ->route('admin-expenses.index')
            ->with('success', 'Expense updated successfully');
    }

    /*
    |--------------------------------------------------------------------------
    | Delete Expense
    |--------------------------------------------------------------------------
    */
    public function destroy(AdminExpense $admin_expense)
    {
        $admin_expense->delete();

        return redirect()
            ->route('admin-expenses.index')
            ->with('success', 'Expense deleted successfully');
    }
}