<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class SuccessPartnersController extends Controller
{
    public function index(Request $request)
    {
        // Get settings from admin panel
        $settings = [];
        $settingsModel = Setting::where('name', 'success_partners')->first();
        if (!empty($settingsModel) && !empty($settingsModel->value)) {
            $settings = json_decode($settingsModel->value, true);
            
            \Log::info('Success Partners Frontend - Loading settings:', [
                'raw_value' => $settingsModel->value,
                'decoded_settings' => $settings,
                'institutions_count' => !empty($settings['institutions']) ? count($settings['institutions']) : 0
            ]);
        }
        $institutionsLimit = !empty($settings['organizations_limit']) ? (int)$settings['organizations_limit'] : 12;
        $showInstitutions = !isset($settings['show_organizations']) || $settings['show_organizations'] == '1';
        $bannerImage = !empty($settings['banner_image']) ? $settings['banner_image'] : '/store/1/default_images/category_cover.png';
        $bannerTitle = !empty($settings['banner_title']) ? $settings['banner_title'] : 'المؤسسات';
        $bannerDescription = !empty($settings['banner_description']) ? $settings['banner_description'] : 'المؤسسات المشاركة';

        // Institutions list from settings
        $institutions = collect($settings['institutions'] ?? [])
            ->map(function ($item) {
                return [
                    'name' => $item['name'] ?? '',
                    'logo' => $item['logo'] ?? '',
                    'country' => $item['country'] ?? '',
                ];
            });

        // Apply search filter for institutions
        if ($request->has('search_inst') && !empty($request->search_inst)) {
            $search = $request->search_inst;
            $institutions = $institutions->filter(function ($institution) use ($search) {
                return mb_stripos($institution['name'], $search) !== false
                    || mb_stripos($institution['country'], $search) !== false;
            });
        }

        $institutionsCount = $institutions->count();
        $institutions = $showInstitutions ? $institutions->take($institutionsLimit)->values() : collect([]);

        // SEO Settings - use admin settings if available, otherwise use default SEO
        $seoSettings = getSeoMetas('success-partners');
        $pageTitle = !empty($settings['seo_title']) ? $settings['seo_title'] : (!empty($seoSettings['title']) ? $seoSettings['title'] : 'المؤسسات');
        $pageDescription = !empty($settings['seo_description']) ? $settings['seo_description'] : (!empty($seoSettings['description']) ? $seoSettings['description'] : 'المؤسسات');
        $pageRobot = getPageRobot('success-partners');

        $data = [
            'title' => $bannerTitle,
            'page' => 'success-partners',
            'pageTitle' => $pageTitle,
            'pageDescription' => $pageDescription,
            'pageRobot' => $pageRobot,
            'institutions' => $institutions,
            'institutionsCount' => $institutionsCount,
            'bannerImage' => $bannerImage,
            'bannerTitle' => $bannerTitle,
            'bannerDescription' => $bannerDescription,
            'showInstitutions' => $showInstitutions,
        ];

        return view('web.default.pages.success_partners', $data);
    }
}

