<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ContentSection;
use Illuminate\Http\Request;

class AboutManagementController extends Controller
{
    /**
     * Display the About page management dashboard
     */
    public function index()
    {
        // Get all About related content sections
        $aboutSections = ContentSection::where('section_key', 'LIKE', 'about_%')
            ->orderBy('section_key')
            ->get();
            
        $sectionStats = [
            'total_sections' => $aboutSections->count(),
            'active_sections' => $aboutSections->where('is_active', true)->count(),
            'inactive_sections' => $aboutSections->where('is_active', false)->count(),
            'last_updated' => $aboutSections->max('updated_at')
        ];
        
        return view('admin.about.index', compact('aboutSections', 'sectionStats'));
    }

    /**
     * Show the form for editing a specific About section
     */
    public function editSection($sectionKey)
    {
        $section = ContentSection::where('section_key', $sectionKey)->firstOrFail();
        
        return view('admin.about.edit-section', compact('section'));
    }
    
    /**
     * Update a specific About section
     */
    public function updateSection(Request $request, $sectionKey)
    {
        $section = ContentSection::where('section_key', $sectionKey)->firstOrFail();
        
        $validated = $request->validate([
            'title' => 'required|string|max:255',
            'content' => 'nullable|string',
            'data' => 'nullable|json',
            'is_active' => 'boolean',
            'sort_order' => 'nullable|integer|min:0'
        ]);
        
        $validated['is_active'] = $request->has('is_active');
        
        $section->update($validated);
        
        return redirect()->route('admin.about.index')
            ->with('success', 'About section updated successfully.');
    }
    
    /**
     * Preview the About page
     */
    public function preview()
    {
        return redirect()->route('about');
    }
}