<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\CaseStudy;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class CaseStudyController extends Controller
{
    public function index() {
        $caseStudies = CaseStudy::orderBy('sort_order')->get();
        return view('admin.pages.case-studies.index', compact('caseStudies'));
    }
    public function create() { return view('admin.pages.case-studies.form'); }
    public function store(Request $request) {
        $request->validate(['title' => 'required']);
        $data = $request->only(['title','tab_label','summary','full_content','category','sort_order','is_active','meta_title','meta_description']);
        $data['slug'] = Str::slug($data['title']);
        if ($request->hasFile('hero_image'))
            $data['hero_image'] = basename($request->file('hero_image')->store('images','public'));
        CaseStudy::create($data);
        return redirect()->route('admin.case-studies.index')->with('success','Case Study saved.');
    }
    public function edit(CaseStudy $caseStudy) { return view('admin.pages.case-studies.form', compact('caseStudy')); }
    public function update(Request $request, CaseStudy $caseStudy) {
        $request->validate(['title' => 'required']);
        $data = $request->only(['title','tab_label','summary','full_content','category','sort_order','is_active','meta_title','meta_description']);
        $data['slug'] = Str::slug($data['title']);
        if ($request->hasFile('hero_image'))
            $data['hero_image'] = basename($request->file('hero_image')->store('images','public'));
        $caseStudy->update($data);
        return redirect()->route('admin.case-studies.index')->with('success','Case Study updated.');
    }
    public function destroy(CaseStudy $caseStudy) {
        $caseStudy->delete();
        return redirect()->route('admin.case-studies.index')->with('success','Case Study deleted.');
    }
}
