<?php

namespace App\Http\Controllers;

use App\Models\AboutSection;
use App\Models\GetInTouch;
use App\Models\HeroSection;
use App\Models\ServiceSection;
use App\Models\WhyChooseUsSection;
use Illuminate\Http\Request;

class SectionController extends Controller
{
    public function storeHero(Request $request)
    {
        // dd($request->all());
        $data = $request->except('_token');

        if ($request->hasFile('image')) {
            $data['image'] = $request->file('image')->store('hero', 'public');
        } else {
            $data['image'] = $request->old_hero_image;
        }

        HeroSection::create($data);

        return back()->with('success', 'Hero saved');
    }

    public function storeAbout(Request $request)
    {
        $data = $request->except('_token');

        if ($request->hasFile('image')) {
            $data['image'] = $request->file('image')->store('about', 'public');
        } else {
            $data['image'] = $request->old_about_image;
        }

        AboutSection::create($data);

        return back()->with('success', 'About saved');
    }

    public function getHero()
    {
        return response()->json([
            'data' => HeroSection::latest()->first(),
        ]);
    }

    public function getAbout()
    {
        return response()->json([
            'status' => true,
            'data' => AboutSection::latest()->first(),
        ]);
    }

    public function index()
    {
        $hero = HeroSection::latest()->first();
        $about = AboutSection::latest()->first();
        $whyChooseUs = WhyChooseUsSection::latest()->first();
        $getInTouch = GetInTouch::latest()->first();
        $service = ServiceSection::latest()->first();

        return view('dashboard', compact('hero', 'about', 'whyChooseUs', 'getInTouch', 'service'));
    }

    // whychoose us
    public function storeWhychooseus(Request $request)
    {
        // dd($request->all());
        $data = $request->except('_token');

        WhyChooseUsSection::create($data);

        return back()->with('success', 'Why Choose Us saved');
    }

    public function getWhyChooseUs()
    {
        return response()->json([
            'status' => true,
            'data' => WhyChooseUsSection::latest()->first(),
        ]);
    }

    public function storeGetInTouch(Request $request)
    {
        // dd($request->all());
        $data = $request->except('_token');
        if ($request->hasFile('image')) {
            $data['image'] = $request->file('image')->store('get_in_touch', 'public');
        } else {
            $data['image'] = $request->old_about_image;
        }
        GetInTouch::create($data);

        return back()->with('success', 'Get in Touch saved');
    }

    public function getGetInTouch()
    {
        return response()->json([
            'status' => true,
            'data' => GetInTouch::latest()->first(),
        ]);
    }

    public function storeService(Request $request)
    {
        $data = $request->except('_token');

        // handle single image if needed
        if ($request->hasFile('image')) {
            $data['image'] = $request->file('image')->store('service_section', 'public');
        } else {
            $data['image'] = $request->old_about_image ?? null;
        }

        // handle all service icons + images properly
        for ($i = 1; $i <= 6; $i++) {

            $iconField = "service_{$i}_icon";
            $imageField = "section_{$i}_image";

            if ($request->hasFile($iconField)) {
                $data[$iconField] = $request->file($iconField)->store('service_section', 'public');
            } else {
                $data[$iconField] = $request->input("old_{$iconField}");
            }

            if ($request->hasFile($imageField)) {
                $data[$imageField] = $request->file($imageField)->store('service_section', 'public');
            } else {
                $data[$imageField] = $request->input("old_{$imageField}");
            }
        }

        ServiceSection::create($data);

        return back()->with('success', 'Service Section saved');
    }

    public function getServices()
    {
        return response()->json([
            'status' => true,
            'data' => ServiceSection::latest()->first(),
        ]);
    }
}
