<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Models\Backend\Journal;
use App\Models\Backend\AboutJournal;
use App\Models\Backend\EditorialCategory;
use App\Models\Backend\ForAuthor;
use App\Models\Backend\Policy;
use App\Models\Backend\JournalInfo;
use Illuminate\Http\Request;

class FrontendController extends Controller
{
    public function index()
    {
        // Fetch the currently active journal (only one should be active)
        $activeJournal = Journal::where('status', '1')->first();

        // Fetch the About Journal information
        $aboutJournal = AboutJournal::first();

        // Pass the data to the view
        return view('frontend.home.index', compact('activeJournal', 'aboutJournal'));
    }

    public function about()
    {
        // Fetch the About Journal information
        $about = AboutJournal::first();

        return view('frontend.about.index', compact('about'));
    }

    // Add this method to your FrontendController
    public function editorialBoard()
    {
        // Fetch active categories ordered by serial_number, with their active members
        $categories = EditorialCategory::where('status', '1')
            ->orderBy('serial_number', 'asc')
            ->with([
                'members' => function ($query) {
                    $query->where('status', '1');
                }
            ])
            ->get();

        return view('frontend.editorial_board.index', compact('categories'));
    }

    public function forAuthors($id = null)
    {
        // Fetch all active "For Author" guidelines ordered by their serial number
        $allAuthorInfos = ForAuthor::where('status', '1')
            ->orderBy('serial', 'asc')
            ->get();

        // If the database is empty, prevent errors by aborting or returning a fallback view
        if ($allAuthorInfos->isEmpty()) {
            abort(404, 'No author instructions found.');
        }

        // If an ID was passed in the URL, fetch that specific record. 
        // Otherwise, default to the first record in our ordered list.
        $currentAuthorInfo = $id ? ForAuthor::findOrFail($id) : $allAuthorInfos->first();

        return view('frontend.for_authors.show', compact('allAuthorInfos', 'currentAuthorInfo'));
    }
    public function policies($id = null)
    {
        // Fetch all active policies ordered by their serial number
        $allPolicies = Policy::where('status', '1')
            ->orderBy('serial', 'asc')
            ->get();

        if ($allPolicies->isEmpty()) {
            abort(404, 'No policies found.');
        }

        // If an ID was passed, fetch that specific policy. Otherwise, default to the first one.
        $currentPolicy = $id ? Policy::findOrFail($id) : $allPolicies->first();

        return view('frontend.policies.show', compact('allPolicies', 'currentPolicy'));
    }

    public function forReviewers($id = null)
    {
        // Fetch all active reviewer guidelines ordered by their serial number
        $allReviewers = \App\Models\Backend\Reviewer::where('status', '1')
            ->orderBy('serial', 'asc')
            ->get();

        if ($allReviewers->isEmpty()) {
            abort(404, 'No reviewer guidelines found.');
        }

        // Default to the first one if no specific ID is provided in the URL
        $currentReviewer = $id ? \App\Models\Backend\Reviewer::findOrFail($id) : $allReviewers->first();

        return view('frontend.for_reviewers.show', compact('allReviewers', 'currentReviewer'));
    }
    public function journalInfo($id = null)
    {

        $allJournalInfos = JournalInfo::where('status', '1')
            ->orderBy('serial', 'asc')
            ->get();

        if ($allJournalInfos->isEmpty()) {
            abort(404, 'No journal information found.');
        }

        // Default to the first page if no specific ID is provided
        $currentInfo = $id ? JournalInfo::findOrFail($id) : $allJournalInfos->first();

        return view('frontend.journal_info.show', compact('allJournalInfos', 'currentInfo'));
    }
    public function contact()
    {

        return view('frontend.contact.index');
    }
}
