<?php

namespace App\Http\Controllers;

use App\Models\SaleSource;
use App\Models\ExpenseCategory;
use Illuminate\Http\Request;

class SettingsController extends Controller
{
    // ─── Sale Sources ────────────────────────────────────────────────────────

    public function saleSources()
    {
        $sources = SaleSource::orderBy('name')->get();
        return view('settings.sale-sources', compact('sources'));
    }

    public function storeSaleSource(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:100|unique:sale_sources,name',
        ]);
        SaleSource::create(['name' => $request->name]);
        return back()->with('success', 'Sale source added.');
    }

    public function destroySaleSource(SaleSource $saleSource)
    {
        $saleSource->delete();
        return back()->with('success', 'Sale source deleted.');
    }

    public function toggleSaleSource(SaleSource $saleSource)
    {
        $saleSource->update(['is_active' => !$saleSource->is_active]);
        return back()->with('success', 'Sale source updated.');
    }

    // ─── Expense Categories ──────────────────────────────────────────────────

    public function expenseCategories()
    {
        $categories = ExpenseCategory::orderBy('name')->get();
        return view('settings.expense-categories', compact('categories'));
    }

    public function storeExpenseCategory(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:100|unique:expense_categories,name',
        ]);
        ExpenseCategory::create(['name' => $request->name]);
        return back()->with('success', 'Expense category added.');
    }

    public function destroyExpenseCategory(ExpenseCategory $expenseCategory)
    {
        $expenseCategory->delete();
        return back()->with('success', 'Expense category deleted.');
    }

    public function toggleExpenseCategory(ExpenseCategory $expenseCategory)
    {
        $expenseCategory->update(['is_active' => !$expenseCategory->is_active]);
        return back()->with('success', 'Expense category updated.');
    }
}
