<?php

namespace App\Http\Controllers;

use App\Models\Story;
use Illuminate\Http\Request;

class StoryController extends Controller
{
    public function index()
    {
        $stories = Story::latest()->get();
        return view('admin.stories.index', compact('stories'));
    }


    public function store(Request $request)
    {
        $request->validate([
            'year' => 'required|integer|min:1900|max:' . date('Y'),
            'story' => 'required|string',
        ]);

        Story::create($request->only(['year', 'story']));

        return response()->json([
            'success' => true,
            'message' => 'Story added successfully.'
        ]);
    }

    public function destroy($id)
    {
        $story = Story::findOrFail($id);
        $story->delete();

        return redirect()->back()->with('success', 'Story deleted successfully.');
    }
}
