<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\PageContent;
use App\Models\PageSeo;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class PageController extends Controller
{
    protected function editPageContent(string $pageKey, string $view, string $title): View
    {
        $record = PageContent::query()->firstOrCreate(['page_key' => $pageKey]);
        $defaultsEn = PageContent::defaultsForPageKey($pageKey, 'en');
        $defaultsAr = PageContent::defaultsForPageKey($pageKey, 'ar');

        $storedEn = is_array($record->data_en) ? $record->data_en : [];
        $storedAr = is_array($record->data_ar) ? $record->data_ar : [];
        $dataEn = ! empty($record->data_en)
            ? array_replace_recursive($defaultsEn, $storedEn)
            : $defaultsEn;
        $dataAr = ! empty($record->data_ar)
            ? array_replace_recursive($defaultsAr, $storedAr)
            : $defaultsAr;

        if ($pageKey === 'full_time_live_in') {
            $dataEn = PageContent::normalizeFullTimeLiveInMerged($dataEn, $defaultsEn, $storedEn);
            $dataAr = PageContent::normalizeFullTimeLiveInMerged($dataAr, $defaultsAr, $storedAr);
        }

        $seo = PageSeo::query()->firstOrCreate(['page_key' => $pageKey]);

        return view($view, [
            'title' => $title,
            'pageKey' => $pageKey,
            'contentEn' => $dataEn,
            'contentAr' => $dataAr,
            'seo' => $seo,
        ]);
    }

    protected function updatePageContent(Request $request, string $pageKey, string $redirectRouteName, string $successMessage): RedirectResponse
    {
        $request->validate([
            'en' => ['required', 'array'],
            'ar' => ['required', 'array'],
            'seo.title_en' => ['nullable', 'string', 'max:255'],
            'seo.title_ar' => ['nullable', 'string', 'max:255'],
            'seo.description_en' => ['nullable', 'string'],
            'seo.description_ar' => ['nullable', 'string'],
            'seo.keywords_en' => ['nullable', 'string'],
            'seo.keywords_ar' => ['nullable', 'string'],
            'seo.heading_en' => ['nullable', 'string', 'max:255'],
            'seo.heading_ar' => ['nullable', 'string', 'max:255'],
            'seo.canonical_url' => ['nullable', 'string', 'max:255'],
            'seo.robots' => ['nullable', 'string', 'max:255'],
            'seo.og_type' => ['nullable', 'string', 'max:255'],
            'seo.twitter_card' => ['nullable', 'string', 'max:255'],
            'seo.schema_json' => ['nullable', 'string'],
        ]);

        $defaultsEn = PageContent::defaultsForPageKey($pageKey, 'en');
        $defaultsAr = PageContent::defaultsForPageKey($pageKey, 'ar');

        $incomingEn = $request->input('en', []);
        $incomingAr = $request->input('ar', []);

        $dataEn = array_replace_recursive($defaultsEn, $incomingEn);
        $dataAr = array_replace_recursive($defaultsAr, $incomingAr);

        if ($pageKey === 'index') {
            $existingIndex = PageContent::query()->where('page_key', 'index')->first();
            $incomingHasSlidesEn = isset($incomingEn['latest_profiles']['slides']) && is_array($incomingEn['latest_profiles']['slides']);
            $incomingHasSlidesAr = isset($incomingAr['latest_profiles']['slides']) && is_array($incomingAr['latest_profiles']['slides']);
            if (! $incomingHasSlidesEn && $existingIndex && is_array($existingIndex->data_en) && isset($existingIndex->data_en['latest_profiles']['slides'])) {
                $dataEn['latest_profiles']['slides'] = $existingIndex->data_en['latest_profiles']['slides'];
            }
            if (! $incomingHasSlidesAr && $existingIndex && is_array($existingIndex->data_ar) && isset($existingIndex->data_ar['latest_profiles']['slides'])) {
                $dataAr['latest_profiles']['slides'] = $existingIndex->data_ar['latest_profiles']['slides'];
            }
        }

        if ($pageKey === 'full_time_live_in') {
            // Do not pass prior DB rows here: merged data is already the submitted form; legacy title-line stripping must not erase new values.
            $dataEn = PageContent::normalizeFullTimeLiveInMerged($dataEn, $defaultsEn, []);
            $dataAr = PageContent::normalizeFullTimeLiveInMerged($dataAr, $defaultsAr, []);
        }

        if (isset($dataEn['contact']['map_points'][3]['href']) && $dataEn['contact']['map_points'][3]['href'] === '') {
            $dataEn['contact']['map_points'][3]['href'] = null;
        }
        if (isset($dataAr['contact']['map_points'][3]['href']) && $dataAr['contact']['map_points'][3]['href'] === '') {
            $dataAr['contact']['map_points'][3]['href'] = null;
        }

        $pageContent = PageContent::query()->firstOrCreate(['page_key' => $pageKey]);
        $pageContent->update([
            'data_en' => $dataEn,
            'data_ar' => $dataAr,
        ]);

        $seoPayload = $request->input('seo', []);
        PageSeo::query()->updateOrCreate(
            ['page_key' => $pageKey],
            $seoPayload
        );

        return redirect()
            ->route($redirectRouteName)
            ->with('success', $successMessage);
    }

    public function editProfiles(Request $request): View
    {
        $pageKey = 'profiles';

        $record = PageContent::query()->firstOrCreate(['page_key' => $pageKey]);
        $defaultsEn = PageContent::defaultsForPageKey($pageKey, 'en');
        $defaultsAr = PageContent::defaultsForPageKey($pageKey, 'ar');

        $dataEn = is_array($record->data_en) && !empty($record->data_en)
            ? array_replace_recursive($defaultsEn, $record->data_en)
            : $defaultsEn;
        $dataAr = is_array($record->data_ar) && !empty($record->data_ar)
            ? array_replace_recursive($defaultsAr, $record->data_ar)
            : $defaultsAr;

        $seo = PageSeo::query()->firstOrCreate(['page_key' => $pageKey]);

        return view('admin.pages.profiles.edit', [
            'title' => 'Profiles',
            'pageKey' => $pageKey,
            'contentEn' => $dataEn,
            'contentAr' => $dataAr,
            'seo' => $seo,
        ]);
    }

    public function updateProfiles(Request $request): RedirectResponse
    {
        $pageKey = 'profiles';

        $defaultsEn = PageContent::defaultsForPageKey($pageKey, 'en');
        $defaultsAr = PageContent::defaultsForPageKey($pageKey, 'ar');

        $request->validate([
            'en.video_modal.title' => ['required', 'string'],
            'en.video_modal.subtitle' => ['required', 'string'],
            'en.video_modal.prev' => ['required', 'string'],
            'en.video_modal.next' => ['required', 'string'],
            'en.video_modal.download' => ['required', 'string'],
            'en.video_modal.share' => ['required', 'string'],
            'en.video_modal.speed' => ['required', 'string'],
            'en.video_modal.close_aria' => ['required', 'string'],

            'en.top.nationality_title' => ['required', 'string'],
            'en.top.selection_showing_prefix' => ['required', 'string'],
            'en.top.all_nationalities' => ['required', 'string'],
            'en.top.search_placeholder' => ['required', 'string'],
            'en.top.candidates_count_singular' => ['required', 'string'],
            'en.top.candidates_count_plural' => ['required', 'string'],

            'en.filters.sponsorship.title' => ['required', 'string'],
            'en.filters.sponsorship.select_title' => ['required', 'string'],
            'en.filters.availability.title' => ['required', 'string'],
            'en.filters.availability.select_title' => ['required', 'string'],
            'en.filters.age.title' => ['required', 'string'],
            'en.filters.age.select_title' => ['required', 'string'],
            'en.filters.experience.title' => ['required', 'string'],
            'en.filters.experience.select_title' => ['required', 'string'],
            'en.filters.language.title' => ['required', 'string'],
            'en.filters.language.select_title' => ['required', 'string'],
            'en.filters.religion.title' => ['required', 'string'],
            'en.filters.religion.select_title' => ['required', 'string'],
            'en.filters.apply' => ['required', 'string'],
            'en.filters.reset' => ['required', 'string'],
            'en.filters.availability_inside' => ['required', 'string'],
            'en.filters.availability_outside' => ['required', 'string'],
            'en.filters.sponsorship_tadbeer' => ['required', 'string'],
            'en.filters.sponsorship_family' => ['required', 'string'],

            'en.empty_state.title' => ['required', 'string'],
            'en.empty_state.description' => ['required', 'string'],

            'en.load_more' => ['required', 'string'],

            'en.cta_band.title' => ['required', 'string'],
            'en.cta_band.description' => ['required', 'string'],
            'en.cta_band.actions' => ['required', 'array', 'size:3'],
            'en.cta_band.actions.*.text' => ['required', 'string'],
            'en.cta_band.actions.*.href' => ['required', 'string'],

            'en.labels.nationality' => ['required', 'string'],
            'en.labels.availability' => ['required', 'string'],
            'en.labels.religion' => ['required', 'string'],
            'en.labels.speaks' => ['required', 'string'],
            'en.labels.marital_status' => ['required', 'string'],
            'en.labels.children' => ['required', 'string'],
            'en.labels.experience' => ['required', 'string'],
            'en.labels.experience_years_suffix' => ['required', 'string'],
            'en.labels.experience_countries' => ['required', 'string'],
            'en.labels.view_cv' => ['required', 'string'],
            'en.labels.salary' => ['required', 'string'],
            'en.labels.hire_me' => ['required', 'string'],
            'en.labels.share_aria' => ['required', 'string'],
            'en.labels.save_aria' => ['required', 'string'],
            'en.labels.play_aria' => ['required', 'string'],

            'en.availability_chip.inside_outside' => ['required', 'string'],
            'en.availability_chip.inside' => ['required', 'string'],
            'en.availability_chip.outside' => ['required', 'string'],

            'en.whatsapp.phone' => ['required', 'string'],
            'en.whatsapp.message_template' => ['required', 'string'],

            'en.contact_section.page.kicker' => ['required', 'string'],
            'en.contact_section.page.heading' => ['required', 'string'],
            'en.contact_section.page.description' => ['required', 'string'],
            'en.contact_section.contact.kicker' => ['nullable', 'string'],
            'en.contact_section.contact.heading' => ['nullable', 'string'],
            'en.contact_section.contact.description' => ['nullable', 'string'],
            'en.contact_section.contact.strip' => ['required', 'array', 'size:4'],
            'en.contact_section.contact.strip.*.label' => ['required', 'string'],
            'en.contact_section.contact.strip.*.href' => ['required', 'string'],
            'en.contact_section.contact.map_title' => ['required', 'string'],
            'en.contact_section.contact.map_iframe_src' => ['required', 'string'],
            'en.contact_section.contact.map_address_html' => ['required', 'string'],
            'en.contact_section.contact.map_points' => ['required', 'array'],
            'en.contact_section.contact.map_points.*.title' => ['required', 'string'],
            'en.contact_section.contact.map_points.*.text' => ['required', 'string'],
            'en.contact_section.contact.map_points.*.href' => ['nullable', 'string'],
            'en.contact_section.contact.form_title' => ['required', 'string'],
            'en.contact_section.contact.form_description' => ['required', 'string'],
            'en.contact_section.contact_form.first_name_placeholder' => ['required', 'string'],
            'en.contact_section.contact_form.last_name_placeholder' => ['required', 'string'],
            'en.contact_section.contact_form.phone_placeholder' => ['required', 'string'],
            'en.contact_section.contact_form.email_placeholder' => ['required', 'string'],
            'en.contact_section.contact_form.message_placeholder' => ['required', 'string'],
            'en.contact_section.contact_form.submit' => ['required', 'string'],

            'ar.video_modal.title' => ['required', 'string'],
            'ar.video_modal.subtitle' => ['required', 'string'],
            'ar.video_modal.prev' => ['required', 'string'],
            'ar.video_modal.next' => ['required', 'string'],
            'ar.video_modal.download' => ['required', 'string'],
            'ar.video_modal.share' => ['required', 'string'],
            'ar.video_modal.speed' => ['required', 'string'],
            'ar.video_modal.close_aria' => ['required', 'string'],

            'ar.top.nationality_title' => ['required', 'string'],
            'ar.top.selection_showing_prefix' => ['required', 'string'],
            'ar.top.all_nationalities' => ['required', 'string'],
            'ar.top.search_placeholder' => ['required', 'string'],
            'ar.top.candidates_count_singular' => ['required', 'string'],
            'ar.top.candidates_count_plural' => ['required', 'string'],

            'ar.filters.sponsorship.title' => ['required', 'string'],
            'ar.filters.sponsorship.select_title' => ['required', 'string'],
            'ar.filters.availability.title' => ['required', 'string'],
            'ar.filters.availability.select_title' => ['required', 'string'],
            'ar.filters.age.title' => ['required', 'string'],
            'ar.filters.age.select_title' => ['required', 'string'],
            'ar.filters.experience.title' => ['required', 'string'],
            'ar.filters.experience.select_title' => ['required', 'string'],
            'ar.filters.language.title' => ['required', 'string'],
            'ar.filters.language.select_title' => ['required', 'string'],
            'ar.filters.religion.title' => ['required', 'string'],
            'ar.filters.religion.select_title' => ['required', 'string'],
            'ar.filters.apply' => ['required', 'string'],
            'ar.filters.reset' => ['required', 'string'],
            'ar.filters.availability_inside' => ['required', 'string'],
            'ar.filters.availability_outside' => ['required', 'string'],
            'ar.filters.sponsorship_tadbeer' => ['required', 'string'],
            'ar.filters.sponsorship_family' => ['required', 'string'],

            'ar.empty_state.title' => ['required', 'string'],
            'ar.empty_state.description' => ['required', 'string'],

            'ar.load_more' => ['required', 'string'],

            'ar.cta_band.title' => ['required', 'string'],
            'ar.cta_band.description' => ['required', 'string'],
            'ar.cta_band.actions' => ['required', 'array', 'size:3'],
            'ar.cta_band.actions.*.text' => ['required', 'string'],
            'ar.cta_band.actions.*.href' => ['required', 'string'],

            'ar.labels.nationality' => ['required', 'string'],
            'ar.labels.availability' => ['required', 'string'],
            'ar.labels.religion' => ['required', 'string'],
            'ar.labels.speaks' => ['required', 'string'],
            'ar.labels.marital_status' => ['required', 'string'],
            'ar.labels.children' => ['required', 'string'],
            'ar.labels.experience' => ['required', 'string'],
            'ar.labels.experience_years_suffix' => ['required', 'string'],
            'ar.labels.experience_countries' => ['required', 'string'],
            'ar.labels.view_cv' => ['required', 'string'],
            'ar.labels.salary' => ['required', 'string'],
            'ar.labels.hire_me' => ['required', 'string'],
            'ar.labels.share_aria' => ['required', 'string'],
            'ar.labels.save_aria' => ['required', 'string'],
            'ar.labels.play_aria' => ['required', 'string'],

            'ar.availability_chip.inside_outside' => ['required', 'string'],
            'ar.availability_chip.inside' => ['required', 'string'],
            'ar.availability_chip.outside' => ['required', 'string'],

            'ar.whatsapp.phone' => ['required', 'string'],
            'ar.whatsapp.message_template' => ['required', 'string'],

            'ar.contact_section.page.kicker' => ['required', 'string'],
            'ar.contact_section.page.heading' => ['required', 'string'],
            'ar.contact_section.page.description' => ['required', 'string'],
            'ar.contact_section.contact.kicker' => ['nullable', 'string'],
            'ar.contact_section.contact.heading' => ['nullable', 'string'],
            'ar.contact_section.contact.description' => ['nullable', 'string'],
            'ar.contact_section.contact.strip' => ['required', 'array', 'size:4'],
            'ar.contact_section.contact.strip.*.label' => ['required', 'string'],
            'ar.contact_section.contact.strip.*.href' => ['required', 'string'],
            'ar.contact_section.contact.map_title' => ['required', 'string'],
            'ar.contact_section.contact.map_iframe_src' => ['required', 'string'],
            'ar.contact_section.contact.map_address_html' => ['required', 'string'],
            'ar.contact_section.contact.map_points' => ['required', 'array'],
            'ar.contact_section.contact.map_points.*.title' => ['required', 'string'],
            'ar.contact_section.contact.map_points.*.text' => ['required', 'string'],
            'ar.contact_section.contact.map_points.*.href' => ['nullable', 'string'],
            'ar.contact_section.contact.form_title' => ['required', 'string'],
            'ar.contact_section.contact.form_description' => ['required', 'string'],
            'ar.contact_section.contact_form.first_name_placeholder' => ['required', 'string'],
            'ar.contact_section.contact_form.last_name_placeholder' => ['required', 'string'],
            'ar.contact_section.contact_form.phone_placeholder' => ['required', 'string'],
            'ar.contact_section.contact_form.email_placeholder' => ['required', 'string'],
            'ar.contact_section.contact_form.message_placeholder' => ['required', 'string'],
            'ar.contact_section.contact_form.submit' => ['required', 'string'],

            'seo.title_en' => ['nullable', 'string', 'max:255'],
            'seo.title_ar' => ['nullable', 'string', 'max:255'],
            'seo.description_en' => ['nullable', 'string'],
            'seo.description_ar' => ['nullable', 'string'],
            'seo.keywords_en' => ['nullable', 'string'],
            'seo.keywords_ar' => ['nullable', 'string'],
            'seo.heading_en' => ['nullable', 'string', 'max:255'],
            'seo.heading_ar' => ['nullable', 'string', 'max:255'],
            'seo.canonical_url' => ['nullable', 'string', 'max:255'],
            'seo.robots' => ['nullable', 'string', 'max:255'],
            'seo.og_type' => ['nullable', 'string', 'max:255'],
            'seo.twitter_card' => ['nullable', 'string', 'max:255'],
            'seo.schema_json' => ['nullable', 'string'],
        ]);

        $incomingEn = $request->input('en', []);
        $incomingAr = $request->input('ar', []);

        $dataEn = array_replace_recursive($defaultsEn, $incomingEn);
        $dataAr = array_replace_recursive($defaultsAr, $incomingAr);

        if (isset($dataEn['contact_section']['contact']['map_points'][3]['href']) && $dataEn['contact_section']['contact']['map_points'][3]['href'] === '') {
            $dataEn['contact_section']['contact']['map_points'][3]['href'] = null;
        }
        if (isset($dataAr['contact_section']['contact']['map_points'][3]['href']) && $dataAr['contact_section']['contact']['map_points'][3]['href'] === '') {
            $dataAr['contact_section']['contact']['map_points'][3]['href'] = null;
        }

        $pageContent = PageContent::query()->firstOrCreate(['page_key' => $pageKey]);
        $pageContent->update([
            'data_en' => $dataEn,
            'data_ar' => $dataAr,
        ]);

        $seoPayload = $request->input('seo', []);
        PageSeo::query()->updateOrCreate(
            ['page_key' => $pageKey],
            $seoPayload
        );

        return redirect()
            ->route('admin.pages.profiles.edit')
            ->with('success', 'Profiles page content updated successfully.');
    }

    public function editProfileDetails(): View
    {
        $pageKey = 'profile_details';

        return $this->editPageContent(
            $pageKey,
            'admin.pages.profile-details.edit',
            'Profile Details'
        );
    }

    public function updateProfileDetails(Request $request): RedirectResponse
    {
        $pageKey = 'profile_details';

        return $this->updatePageContent(
            $request,
            $pageKey,
            'admin.pages.profile-details.edit',
            'Profile details page content updated successfully.'
        );
    }

    public function editFullTimeLiveIn(): View
    {
        $pageKey = 'full_time_live_in';

        return $this->editPageContent(
            $pageKey,
            'admin.pages.full-time-live-in.edit',
            'Full Time Live-In'
        );
    }

    public function updateFullTimeLiveIn(Request $request): RedirectResponse
    {
        $pageKey = 'full_time_live_in';

        return $this->updatePageContent(
            $request,
            $pageKey,
            'admin.pages.full-time-live-in.edit',
            'Full time live-in page content updated successfully.'
        );
    }

    public function editIndex(): View
    {
        return $this->editPageContent(
            'index',
            'admin.pages.index.edit',
            'Home'
        );
    }

    public function updateIndex(Request $request): RedirectResponse
    {
        return $this->updatePageContent(
            $request,
            'index',
            'admin.pages.index.edit',
            'Home page content updated successfully.'
        );
    }

    public function editPackages(): View
    {
        return $this->editPageContent(
            'packages',
            'admin.pages.packages.edit',
            'Packages'
        );
    }

    public function updatePackages(Request $request): RedirectResponse
    {
        return $this->updatePageContent(
            $request,
            'packages',
            'admin.pages.packages.edit',
            'Packages page content updated successfully.'
        );
    }

    public function editCandidatesProfiles(Request $request): View
    {
        $pageKey = 'candidates_profiles';

        $record = PageContent::query()->firstOrCreate(['page_key' => $pageKey]);
        $defaults = PageContent::defaultsForPageKey($pageKey, 'en');
        $defaultsAr = PageContent::defaultsForPageKey($pageKey, 'ar');

        $dataEn = is_array($record->data_en) && !empty($record->data_en)
            ? array_replace_recursive($defaults, $record->data_en)
            : $defaults;
        $dataAr = is_array($record->data_ar) && !empty($record->data_ar)
            ? array_replace_recursive($defaultsAr, $record->data_ar)
            : $defaultsAr;

        $seo = PageSeo::query()->firstOrCreate(['page_key' => $pageKey]);

        return view('admin.pages.candidates-profiles.edit', [
            'title' => 'Candidates Profiles',
            'pageKey' => $pageKey,
            'contentEn' => $dataEn,
            'contentAr' => $dataAr,
            'seo' => $seo,
        ]);
    }

    public function updateCandidatesProfiles(Request $request): RedirectResponse
    {
        $pageKey = 'candidates_profiles';

        $defaultsEn = PageContent::defaultsForPageKey($pageKey, 'en');
        $defaultsAr = PageContent::defaultsForPageKey($pageKey, 'ar');

        $request->validate([
            'en.hero_note' => ['required', 'string'],
            'en.company_reason.title' => ['required', 'string'],
            'en.company_reason.items' => ['required', 'array', 'size:4'],
            'en.company_reason.items.*' => ['required', 'string'],

            'en.company_plans.uae.title' => ['required', 'string'],
            'en.company_plans.uae.price_amount' => ['required', 'string'],
            'en.company_plans.uae.price_suffix' => ['required', 'string'],
            'en.company_plans.uae.items' => ['required', 'array', 'size:1'],
            'en.company_plans.uae.items.*' => ['required', 'string'],
            'en.company_plans.uae.cta.text' => ['required', 'string'],
            'en.company_plans.uae.cta.href' => ['required', 'string'],

            'en.company_plans.overseas.title' => ['required', 'string'],
            'en.company_plans.overseas.price_amount' => ['required', 'string'],
            'en.company_plans.overseas.price_suffix' => ['required', 'string'],
            'en.company_plans.overseas.items' => ['required', 'array', 'size:1'],
            'en.company_plans.overseas.items.*' => ['required', 'string'],
            'en.company_plans.overseas.cta.text' => ['required', 'string'],
            'en.company_plans.overseas.cta.href' => ['required', 'string'],

            'en.family_reason.title' => ['required', 'string'],
            'en.family_reason.items' => ['required', 'array', 'size:4'],
            'en.family_reason.items.*' => ['required', 'string'],

            'en.family_plans.uae.title' => ['required', 'string'],
            'en.family_plans.uae.price_amount' => ['required', 'string'],
            'en.family_plans.uae.price_suffix' => ['required', 'string'],
            'en.family_plans.uae.items' => ['required', 'array', 'size:1'],
            'en.family_plans.uae.items.*' => ['required', 'string'],
            'en.family_plans.uae.cta.text' => ['required', 'string'],
            'en.family_plans.uae.cta.href' => ['required', 'string'],

            'en.family_plans.overseas.title' => ['required', 'string'],
            'en.family_plans.overseas.price_amount' => ['required', 'string'],
            'en.family_plans.overseas.price_suffix' => ['required', 'string'],
            'en.family_plans.overseas.items' => ['required', 'array', 'size:1'],
            'en.family_plans.overseas.items.*' => ['required', 'string'],
            'en.family_plans.overseas.cta.text' => ['required', 'string'],
            'en.family_plans.overseas.cta.href' => ['required', 'string'],

            'en.hero_choice_actions' => ['required', 'array', 'size:3'],
            'en.hero_choice_actions.0.text' => ['required', 'string'],
            'en.hero_choice_actions.0.href' => ['required', 'string'],
            'en.hero_choice_actions.1.text' => ['required', 'string'],
            'en.hero_choice_actions.1.href' => ['required', 'string'],
            'en.hero_choice_actions.2.text' => ['required', 'string'],
            'en.hero_choice_actions.2.href' => ['required', 'string'],

            'en.cta_band.title' => ['required', 'string'],
            'en.cta_band.description' => ['required', 'string'],
            'en.cta_band.actions' => ['required', 'array', 'size:3'],
            'en.cta_band.actions.0.text' => ['required', 'string'],
            'en.cta_band.actions.0.href' => ['required', 'string'],
            'en.cta_band.actions.1.text' => ['required', 'string'],
            'en.cta_band.actions.1.href' => ['required', 'string'],
            'en.cta_band.actions.2.text' => ['required', 'string'],
            'en.cta_band.actions.2.href' => ['required', 'string'],

            'en.faq.kicker' => ['required', 'string'],
            'en.faq.title' => ['required', 'string'],
            'en.faq.description' => ['required', 'string'],
            'en.faq.items' => ['required', 'array', 'size:5'],
            'en.faq.items.*.question' => ['required', 'string'],
            'en.faq.items.*.answer' => ['required', 'string'],

            'en.contact.kicker' => ['required', 'string'],
            'en.contact.title' => ['required', 'string'],
            'en.contact.description' => ['required', 'string'],
            'en.contact.map_panel.title' => ['required', 'string'],
            'en.contact.map_panel.description' => ['required', 'string'],
            'en.contact.map_panel.iframe_src' => ['required', 'string'],

            'en.contact.contact_points' => ['required', 'array', 'size:4'],
            'en.contact.contact_points.*.title' => ['required', 'string'],
            'en.contact.contact_points.*.text' => ['required', 'string'],
            'en.contact.contact_points.0.href' => ['required', 'string'],
            'en.contact.contact_points.1.href' => ['required', 'string'],
            'en.contact.contact_points.2.href' => ['required', 'string'],
            'en.contact.contact_points.3.href' => ['nullable', 'string'],

            'en.contact.form_panel.title' => ['required', 'string'],
            'en.contact.form_panel.description' => ['required', 'string'],
            'en.contact.form_panel.name_placeholder' => ['required', 'string'],
            'en.contact.form_panel.phone_placeholder' => ['required', 'string'],
            'en.contact.form_panel.email_placeholder' => ['required', 'string'],
            'en.contact.form_panel.inquiry_type_placeholder' => ['required', 'string'],
            'en.contact.form_panel.inquiry_type_options' => ['required', 'array', 'size:4'],
            'en.contact.form_panel.inquiry_type_options.*' => ['required', 'string'],
            'en.contact.form_panel.message_placeholder' => ['required', 'string'],
            'en.contact.form_panel.submit_text' => ['required', 'string'],

            'en.contact.contact_actions' => ['required', 'array', 'size:4'],
            'en.contact.contact_actions.*.text' => ['required', 'string'],
            'en.contact.contact_actions.*.href' => ['required', 'string'],

            'ar.hero_note' => ['required', 'string'],
            'ar.company_reason.title' => ['required', 'string'],
            'ar.company_reason.items' => ['required', 'array', 'size:4'],
            'ar.company_reason.items.*' => ['required', 'string'],

            'ar.company_plans.uae.title' => ['required', 'string'],
            'ar.company_plans.uae.price_amount' => ['required', 'string'],
            'ar.company_plans.uae.price_suffix' => ['required', 'string'],
            'ar.company_plans.uae.items' => ['required', 'array', 'size:1'],
            'ar.company_plans.uae.items.*' => ['required', 'string'],
            'ar.company_plans.uae.cta.text' => ['required', 'string'],
            'ar.company_plans.uae.cta.href' => ['required', 'string'],

            'ar.company_plans.overseas.title' => ['required', 'string'],
            'ar.company_plans.overseas.price_amount' => ['required', 'string'],
            'ar.company_plans.overseas.price_suffix' => ['required', 'string'],
            'ar.company_plans.overseas.items' => ['required', 'array', 'size:1'],
            'ar.company_plans.overseas.items.*' => ['required', 'string'],
            'ar.company_plans.overseas.cta.text' => ['required', 'string'],
            'ar.company_plans.overseas.cta.href' => ['required', 'string'],

            'ar.family_reason.title' => ['required', 'string'],
            'ar.family_reason.items' => ['required', 'array', 'size:4'],
            'ar.family_reason.items.*' => ['required', 'string'],

            'ar.family_plans.uae.title' => ['required', 'string'],
            'ar.family_plans.uae.price_amount' => ['required', 'string'],
            'ar.family_plans.uae.price_suffix' => ['required', 'string'],
            'ar.family_plans.uae.items' => ['required', 'array', 'size:1'],
            'ar.family_plans.uae.items.*' => ['required', 'string'],
            'ar.family_plans.uae.cta.text' => ['required', 'string'],
            'ar.family_plans.uae.cta.href' => ['required', 'string'],

            'ar.family_plans.overseas.title' => ['required', 'string'],
            'ar.family_plans.overseas.price_amount' => ['required', 'string'],
            'ar.family_plans.overseas.price_suffix' => ['required', 'string'],
            'ar.family_plans.overseas.items' => ['required', 'array', 'size:1'],
            'ar.family_plans.overseas.items.*' => ['required', 'string'],
            'ar.family_plans.overseas.cta.text' => ['required', 'string'],
            'ar.family_plans.overseas.cta.href' => ['required', 'string'],

            'ar.hero_choice_actions' => ['required', 'array', 'size:3'],
            'ar.hero_choice_actions.0.text' => ['required', 'string'],
            'ar.hero_choice_actions.0.href' => ['required', 'string'],
            'ar.hero_choice_actions.1.text' => ['required', 'string'],
            'ar.hero_choice_actions.1.href' => ['required', 'string'],
            'ar.hero_choice_actions.2.text' => ['required', 'string'],
            'ar.hero_choice_actions.2.href' => ['required', 'string'],

            'ar.cta_band.title' => ['required', 'string'],
            'ar.cta_band.description' => ['required', 'string'],
            'ar.cta_band.actions' => ['required', 'array', 'size:3'],
            'ar.cta_band.actions.0.text' => ['required', 'string'],
            'ar.cta_band.actions.0.href' => ['required', 'string'],
            'ar.cta_band.actions.1.text' => ['required', 'string'],
            'ar.cta_band.actions.1.href' => ['required', 'string'],
            'ar.cta_band.actions.2.text' => ['required', 'string'],
            'ar.cta_band.actions.2.href' => ['required', 'string'],

            'ar.faq.kicker' => ['required', 'string'],
            'ar.faq.title' => ['required', 'string'],
            'ar.faq.description' => ['required', 'string'],
            'ar.faq.items' => ['required', 'array', 'size:5'],
            'ar.faq.items.*.question' => ['required', 'string'],
            'ar.faq.items.*.answer' => ['required', 'string'],

            'ar.contact.kicker' => ['required', 'string'],
            'ar.contact.title' => ['required', 'string'],
            'ar.contact.description' => ['required', 'string'],
            'ar.contact.map_panel.title' => ['required', 'string'],
            'ar.contact.map_panel.description' => ['required', 'string'],
            'ar.contact.map_panel.iframe_src' => ['required', 'string'],

            'ar.contact.contact_points' => ['required', 'array', 'size:4'],
            'ar.contact.contact_points.*.title' => ['required', 'string'],
            'ar.contact.contact_points.*.text' => ['required', 'string'],
            'ar.contact.contact_points.0.href' => ['required', 'string'],
            'ar.contact.contact_points.1.href' => ['required', 'string'],
            'ar.contact.contact_points.2.href' => ['required', 'string'],
            'ar.contact.contact_points.3.href' => ['nullable', 'string'],

            'ar.contact.form_panel.title' => ['required', 'string'],
            'ar.contact.form_panel.description' => ['required', 'string'],
            'ar.contact.form_panel.name_placeholder' => ['required', 'string'],
            'ar.contact.form_panel.phone_placeholder' => ['required', 'string'],
            'ar.contact.form_panel.email_placeholder' => ['required', 'string'],
            'ar.contact.form_panel.inquiry_type_placeholder' => ['required', 'string'],
            'ar.contact.form_panel.inquiry_type_options' => ['required', 'array', 'size:4'],
            'ar.contact.form_panel.inquiry_type_options.*' => ['required', 'string'],
            'ar.contact.form_panel.message_placeholder' => ['required', 'string'],
            'ar.contact.form_panel.submit_text' => ['required', 'string'],

            'ar.contact.contact_actions' => ['required', 'array', 'size:4'],
            'ar.contact.contact_actions.*.text' => ['required', 'string'],
            'ar.contact.contact_actions.*.href' => ['required', 'string'],

            'seo.title_en' => ['nullable', 'string', 'max:255'],
            'seo.title_ar' => ['nullable', 'string', 'max:255'],
            'seo.description_en' => ['nullable', 'string'],
            'seo.description_ar' => ['nullable', 'string'],
            'seo.keywords_en' => ['nullable', 'string'],
            'seo.keywords_ar' => ['nullable', 'string'],
            'seo.heading_en' => ['nullable', 'string', 'max:255'],
            'seo.heading_ar' => ['nullable', 'string', 'max:255'],
            'seo.canonical_url' => ['nullable', 'string', 'max:255'],
            'seo.robots' => ['nullable', 'string', 'max:255'],
            'seo.og_type' => ['nullable', 'string', 'max:255'],
            'seo.twitter_card' => ['nullable', 'string', 'max:255'],
            'seo.schema_json' => ['nullable', 'string'],
        ]);

        $incomingEn = $request->input('en', []);
        $incomingAr = $request->input('ar', []);

        $dataEn = array_replace_recursive($defaultsEn, $incomingEn);
        $dataAr = array_replace_recursive($defaultsAr, $incomingAr);

        // Normalize empty hrefs for "Address" tile (contact_points[3])
        if (isset($dataEn['contact']['contact_points'][3]['href']) && $dataEn['contact']['contact_points'][3]['href'] === '') {
            $dataEn['contact']['contact_points'][3]['href'] = null;
        }

        if (isset($dataAr['contact']['contact_points'][3]['href']) && $dataAr['contact']['contact_points'][3]['href'] === '') {
            $dataAr['contact']['contact_points'][3]['href'] = null;
        }

        $pageContent = PageContent::query()->firstOrCreate(['page_key' => $pageKey]);
        $pageContent->update([
            'data_en' => $dataEn,
            'data_ar' => $dataAr,
        ]);

        $seoPayload = $request->input('seo', []);
        PageSeo::query()->updateOrCreate(
            ['page_key' => $pageKey],
            $seoPayload
        );

        return redirect()
            ->route('admin.pages.candidates-profiles.edit')
            ->with('success', 'Candidates profiles content updated successfully.');
    }

    public function editServices(): View
    {
        return view('admin.placeholder', ['title' => 'Services']);
    }

    public function updateServices(Request $request): RedirectResponse
    {
        return back()->with('success', 'Services page update is not implemented yet.');
    }

    public function editAboutUs(): View
    {
        return $this->editPageContent(
            'about_us',
            'admin.pages.about-us.edit',
            'About Us'
        );
    }

    public function updateAboutUs(Request $request): RedirectResponse
    {
        return $this->updatePageContent(
            $request,
            'about_us',
            'admin.pages.about-us.edit',
            'About Us page content updated successfully.'
        );
    }

    public function editMaidVisa(): View
    {
        return $this->editPageContent(
            'maid_visa',
            'admin.pages.maid-visa.edit',
            'Maid Visa'
        );
    }

    public function updateMaidVisa(Request $request): RedirectResponse
    {
        return $this->updatePageContent(
            $request,
            'maid_visa',
            'admin.pages.maid-visa.edit',
            'Maid Visa page content updated successfully.'
        );
    }

    public function editContactUs(): View
    {
        return $this->editPageContent(
            'contact_us',
            'admin.pages.contact-us.edit',
            'Contact Us'
        );
    }

    public function updateContactUs(Request $request): RedirectResponse
    {
        return $this->updatePageContent(
            $request,
            'contact_us',
            'admin.pages.contact-us.edit',
            'Contact Us page content updated successfully.'
        );
    }

    public function editTermsAndConditions(): View
    {
        return $this->editPageContent(
            'terms_and_conditions',
            'admin.pages.terms-and-conditions.edit',
            'Terms and Conditions'
        );
    }

    public function updateTermsAndConditions(Request $request): RedirectResponse
    {
        return $this->updatePageContent(
            $request,
            'terms_and_conditions',
            'admin.pages.terms-and-conditions.edit',
            'Terms and Conditions page updated successfully.'
        );
    }

    public function editPrivacyPolicy(): View
    {
        return $this->editPageContent(
            'privacy_policy',
            'admin.pages.privacy-policy.edit',
            'Privacy Policy'
        );
    }

    public function updatePrivacyPolicy(Request $request): RedirectResponse
    {
        return $this->updatePageContent(
            $request,
            'privacy_policy',
            'admin.pages.privacy-policy.edit',
            'Privacy Policy page updated successfully.'
        );
    }
}

