<?php

namespace App\Http\Controllers;

use App\Models\SavingsProduct;
use App\Http\Requests\StoreSavingsProductRequest;
use App\Http\Requests\UpdateSavingsProductRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class SavingsProductController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $savingsProducts = SavingsProduct::with('creator')
            ->orderBy('created_at', 'desc')
            ->paginate(15);

        return view('savings.products.index', compact('savingsProducts'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('savings.products.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreSavingsProductRequest $request)
    {
        $validated = $request->validated();
        $validated['created_by'] = Auth::id();

        SavingsProduct::create($validated);

        return redirect()->route('savings.products.index')
            ->with('success', 'Savings product created successfully.');
    }

    /**
     * Display the specified resource.
     */
    public function show(SavingsProduct $savingsProduct)
    {
        $savingsProduct->load('creator', 'savingsAccounts');

        return view('savings.products.show', compact('savingsProduct'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(SavingsProduct $savingsProduct)
    {
        return view('savings.products.edit', compact('savingsProduct'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateSavingsProductRequest $request, SavingsProduct $savingsProduct)
    {
        $validated = $request->validated();

        $savingsProduct->update($validated);

        return redirect()->route('savings.products.index')
            ->with('success', 'Savings product updated successfully.');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(SavingsProduct $savingsProduct)
    {
        // Check if product has associated accounts
        if ($savingsProduct->savingsAccounts()->count() > 0) {
            return redirect()->route('savings.products.index')
                ->with('error', 'Cannot delete product with associated savings accounts.');
        }

        $savingsProduct->delete();

        return redirect()->route('savings.products.index')
            ->with('success', 'Savings product deleted successfully.');
    }

    /**
     * Toggle product status
     */
    public function toggleStatus(SavingsProduct $savingsProduct)
    {
        $savingsProduct->update([
            'is_active' => !$savingsProduct->is_active
        ]);

        $status = $savingsProduct->is_active ? 'activated' : 'deactivated';

        return redirect()->route('savings.products.index')
            ->with('success', "Savings product {$status} successfully.");
    }
}
