<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Department;
use App\Models\MenuItem;
use App\Models\DonationSetting;
use Illuminate\Support\Str;

class ChurchController extends Controller
{
    public function index()
    {
        return view('home');
    }

    public function about()
    {
        return view('about');
    }

    public function branches()
    {
        return view('branches');
    }

    public function services()
    {
        return view('services');
    }

    public function contact()
    {
        return view('contact');
    }

    public function departments()
    {
        $departments = Department::where('is_active', true)->get();
        return view('departments.index', compact('departments'));
    }

    public function department($slug)
    {
        $department = Department::where('slug', $slug)->firstOrFail();
        return view('departments.show', ['department' => $department]);
    }

    public function leadership()
    {
        $leaders = [
            'pastors' => [
                [
                    'name' => 'D/S Pastor Gideon Adeoye',
                    'title' => 'District Superintendent CAC The Gambia',
                    'category' => 'Leadership',
                    'bio' => 'D/S Pastor Gideon Ademola Adeoye is a missionary called through the will of God to preach the Gospel in many nations around the world. He was posted to The Gambia on full foreign missions in 2001. He is committed to planting indigenous churches.',
                    'image' => 'Picture1.jpg',
                    'phone' => '+220 7781554 / 9862413',
                    'email' => 'adeoyega@gmail.com'
                ],
                [
                    'name' => 'Pastor Massanneh Kolly',
                    'title' => 'Pastor',
                    'category' => 'Pastoral',
                    'bio' => 'Pastor Massanneh Kolly serves at CAC The Gambia with dedication and commitment.',
                    'image' => 'avatar.png',
                    'phone' => '+220',
                    'email' => ''
                ],
                [
                    'name' => 'Pastor John Oyewale',
                    'title' => 'Currate',
                    'category' => 'Pastoral',
                    'bio' => 'Pastor John Oyewale serves as the Currate at CAC The Gambia.',
                    'image' => 'pst_john.jpeg',
                    'phone' => '+220',
                    'email' => ''
                ],
                [
                    'name' => 'Pastor Anthony Mendy',
                    'title' => 'Natrinal',
                    'category' => 'Pastoral',
                    'bio' => 'Pastor Anthony Mendy serves at CAC The Gambia.',
                    'image' => 'pst_anthony.jpeg',
                    'phone' => '+220',
                    'email' => ''
                ],
                [
                    'name' => 'Pastor Raymond Conedera',
                    'title' => 'Revivalist',
                    'category' => 'Pastoral',
                    'bio' => 'Pastor Raymond Conedera serves as Revivalist at CAC The Gambia.',
                    'image' => 'pst_raymond.jpeg',
                    'phone' => '+220',
                    'email' => ''
                ],
                [
                    'name' => 'Evangelist Kelvin Pkhuarobo',
                    'title' => 'Evangelist',
                    'category' => 'Evangelists',
                    'bio' => 'Evangelist Kelvin Pkhuarobo serves at CAC The Gambia.',
                    'image' => 'evan_kelvin.jpeg',
                    'phone' => '+220',
                    'email' => ''
                ],
                [
                    'name' => 'Evangelist John Mendy',
                    'title' => 'Evangelist',
                    'category' => 'Evangelists',
                    'bio' => 'Evangelist John Mendy serves at CAC The Gambia.',
                    'image' => 'avatar.png',
                    'phone' => '+220',
                    'email' => ''
                ],
                [
                    'name' => 'Evangelist Peter Mendy',
                    'title' => 'Evangelist',
                    'category' => 'Evangelists',
                    'bio' => 'Evangelist Peter Mendy serves at CAC The Gambia - Prayer Camp Ground, Mayama Village.',
                    'image' => 'evang_peter.jpeg',
                    'phone' => '+220',
                    'email' => ''
                ],
                [
                    'name' => 'Evangelist David Osarenren',
                    'title' => 'Evangelist',
                    'category' => 'Evangelists',
                    'bio' => 'Evangelist David Osarenren serves at CAC The Gambia - Lamin Branch.',
                    'image' => 'avatar.png',
                    'phone' => '+220 7883763',
                    'email' => ''
                ],
                [
                    'name' => 'Bro. Abraham Mendy',
                    'title' => 'Brother',
                    'category' => 'Brothers',
                    'bio' => 'Bro. Abraham Mendy serves at CAC The Gambia.',
                    'image' => 'evang_abraham.jpeg',
                    'phone' => '+220',
                    'email' => ''
                ],
                [
                    'name' => 'Bro. Sandy Vibbie',
                    'title' => 'Brother',
                    'category' => 'Brothers',
                    'bio' => 'Bro. Sandy Vibbie serves at CAC The Gambia.',
                    'image' => 'evan_sandy.jpeg',
                    'phone' => '+220',
                    'email' => ''
                ],
                [
                    'name' => 'Evangelist Senseh Jatta',
                    'title' => 'Evangelist',
                    'category' => 'Evangelists',
                    'bio' => 'Evangelist Senseh Jatta serves at CAC The Gambia.',
                    'image' => 'avatar.png',
                    'phone' => '+220',
                    'email' => ''
                ],
            ],
            'elders' => [
                ['name' => 'Elder Isaac Oyeniyi', 'title' => 'Elder', 'image' => 'avatar.png'],
                ['name' => 'Sunday Akinwale', 'title' => 'Elder', 'image' => 'avatar.png'],
                ['name' => 'Samuel Ajayi', 'title' => 'Elder', 'image' => 'avatar.png'],
                ['name' => 'James Mendy', 'title' => 'Elder', 'image' => 'avatar.png'],
                ['name' => 'Samson Adeoye', 'title' => 'Elder', 'image' => 'avatar.png'],
                ['name' => 'Alex Bolou', 'title' => 'Elder', 'image' => 'avatar.png'],
            ],
            'deaconesses' => [
                ['name' => 'Deaconess Sebastin Bada', 'title' => 'Deaconess', 'image' => 'avatar.png'],
                ['name' => 'Deaconess Rachael Ademesi', 'title' => 'Deaconess', 'image' => 'avatar.png'],
                ['name' => 'Deaconess Modupe Adeoye', 'title' => 'Deaconess', 'image' => 'avatar.png'],
                ['name' => 'Deaconess Roze Conedera', 'title' => 'Deaconess', 'image' => 'avatar.png'],
                ['name' => 'Deaconess Lonica Bolou', 'title' => 'Deaconess', 'image' => 'avatar.png'],
                ['name' => 'Deaconess Nene', 'title' => 'Deaconess', 'image' => 'avatar.png'],
            ]
        ];

        return view('leadership', ['leaders' => $leaders]);
    }

    public function pastors()
    {
        return redirect()->route('leadership');
    }

    // Admin Methods
    public function adminDepartments()
    {
        $departments = Department::orderBy('name')->get();
        return view('admin.departments.index', compact('departments'));
    }

    public function adminStoreDepartment(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'required|string',
            'activities' => 'nullable|string',
            'age' => 'nullable|string|max:100',
            'meeting' => 'nullable|string|max:255',
            'image' => 'nullable|string|max:255',
        ]);

        $validated['slug'] = \Str::slug($request->name);
        
        Department::create($validated);

        return redirect()->route('admin.departments')->with('success', 'Department created successfully!');
    }

    public function adminEditDepartment($id)
    {
        $department = Department::findOrFail($id);
        return view('admin.departments.edit', compact('department'));
    }

    public function adminUpdateDepartment(Request $request, $id)
    {
        $department = Department::findOrFail($id);

        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'required|string',
            'activities' => 'nullable|string',
            'age' => 'nullable|string|max:100',
            'meeting' => 'nullable|string|max:255',
            'image' => 'nullable|string|max:255',
            'is_active' => 'boolean',
        ]);

        $validated['slug'] = \Str::slug($request->name);
        $validated['is_active'] = $request->has('is_active');

        $department->update($validated);

        return redirect()->route('admin.departments')->with('success', 'Department updated successfully!');
    }

    public function adminDestroyDepartment($id)
    {
        $department = Department::findOrFail($id);
        $department->delete();

        return redirect()->route('admin.departments')->with('success', 'Department deleted successfully!');
    }

    // Menu Management
    public function adminMenu()
    {
        $menuItems = MenuItem::whereNull('parent_id')->with('children')->orderBy('order')->get();
        $allMenuItems = MenuItem::orderBy('name')->get();
        return view('admin.menu.index', compact('menuItems', 'allMenuItems'));
    }

    public function adminStoreMenu(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'slug' => 'nullable|string|max:255',
            'url' => 'required|string|max:255',
            'icon' => 'nullable|string|max:100',
            'parent_id' => 'nullable|exists:menu_items,id',
            'order' => 'integer|min:0',
        ]);

        $validated['is_active'] = $request->has('is_active');

        MenuItem::create($validated);

        return redirect()->route('admin.menu')->with('success', 'Menu item created successfully!');
    }

    public function adminEditMenu($id)
    {
        $menuItem = MenuItem::findOrFail($id);
        $allMenuItems = MenuItem::where('id', '!=', $id)->orderBy('name')->get();
        return view('admin.menu.edit', compact('menuItem', 'allMenuItems'));
    }

    public function adminUpdateMenu(Request $request, $id)
    {
        $menuItem = MenuItem::findOrFail($id);

        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'slug' => 'nullable|string|max:255',
            'url' => 'required|string|max:255',
            'icon' => 'nullable|string|max:100',
            'parent_id' => 'nullable|exists:menu_items,id',
            'order' => 'integer|min:0',
        ]);

        $validated['is_active'] = $request->has('is_active');

        $menuItem->update($validated);

        return redirect()->route('admin.menu')->with('success', 'Menu item updated successfully!');
    }

    public function adminDestroyMenu($id)
    {
        $menuItem = MenuItem::findOrFail($id);
        $menuItem->delete();

        return redirect()->route('admin.menu')->with('success', 'Menu item deleted successfully!');
    }

    // Donation Settings
    public function donation()
    {
        $donation = DonationSetting::first();
        return view('donation', compact('donation'));
    }

    public function adminDonation()
    {
        $donation = DonationSetting::first() ?? new DonationSetting();
        return view('admin.donation.index', compact('donation'));
    }

    public function adminUpdateDonation(Request $request)
    {
        $validated = $request->validate([
            'bank_name' => 'nullable|string|max:255',
            'account_name' => 'nullable|string|max:255',
            'account_number' => 'nullable|string|max:100',
            'sort_code' => 'nullable|string|max:100',
            'mobile_money' => 'nullable|string|max:100',
            'mobile_provider' => 'nullable|string|max:255',
        ]);

        $donation = DonationSetting::first();
        
        if ($donation) {
            $donation->update($validated);
        } else {
            DonationSetting::create($validated);
        }

        return redirect()->route('admin.donation')->with('success', 'Donation settings updated successfully!');
    }
}
