<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Hero;
use App\Models\PricingPlan;

class HeroController extends Controller
{
    public function index()
    {
        $heroes = Hero::orderBy('id', 'asc')->get();

        $hero_map = [];
        foreach ($heroes as $hero) {
            $content = isset($hero->content) ? (is_array($hero->content) ? $hero->content : json_decode($hero->content, true)) : null;

            // Sort pricing plans by speed if they exist
            if ($content && isset($content['pricing_plans'])) {
                $plans = $content['pricing_plans'];
                usort($plans, function($a, $b) {
                    // Extract speed values using regex
                    preg_match('/(\d+)\s*Mbps/', $a, $speedA);
                    preg_match('/(\d+)\s*Mbps/', $b, $speedB);

                    $speedA = isset($speedA[1]) ? (int)$speedA[1] : 0;
                    $speedB = isset($speedB[1]) ? (int)$speedB[1] : 0;

                    return $speedA - $speedB;
                });
                $content['pricing_plans'] = $plans;
            }

            $hero_map[$hero->id] = [
                'id' => $hero->id,
                'name' => $hero->name,
                'bg_image' => isset($hero->bg_image) ? getObjUrl('hero', $hero->bg_image) : null,
                'image' => isset($hero->image) ? getObjUrl('hero', $hero->image) : null,
                'content' => $content
            ];
        }

        return sendSuccessResponse($hero_map);
    }
    public function edit($id)
    {
        
        $hero = Hero::find($id);
        $package = PricingPlan::all();
        if ($id == 1) {
            $view = 'Backend.Hero.edit1';
        } elseif ($id == 2) {
            $view = 'Backend.Hero.edit2';
        } elseif ($id == 3) {
            $view = 'Backend.Hero.edit3';
        } elseif ($id == 4) {
            $view = 'Backend.Hero.edit4';
        } else {
            $view = 'Backend.Hero.edit5';
        }

        return view($view, ['hero' => $hero, 'package' => $package]);
    }

    public function update1(Request $request, $id)
    {
        $request->validate([
            'title' => 'required|max:64',
            'feature_url' => 'required|url',
            'icon_url_1' => 'required|url',
            'icon_url_2' => 'required|url',
            'icon_url_3' => 'required|url',
            'icon_url_4' => 'required|url',
            'icon_url_5' => 'required|url',
            'icon_url_6' => 'required|url',
            'icon_url_7' => 'required|url',
            'icon_url_8' => 'nullable|url',
            'background_image' => 'sometimes|image|mimes:jpeg,png,jpg,gif,webp,svg',
            'feature_image' => 'sometimes|image|mimes:jpeg,png,jpg,gif,webp,svg'
        ]);

        try {
            // Format data for JSON storage
            $jsonData = [
                'title' => $request->title,
                'feature_url' => $request->feature_url,
                'icons' => [
                    $request->icon_url_1,
                    $request->icon_url_2,
                    $request->icon_url_3,
                    $request->icon_url_4,
                    $request->icon_url_5,
                    $request->icon_url_6,
                    $request->icon_url_7
                ]
            ];

            // Add optional 8th icon if provided
            if ($request->icon_url_8) {
                $jsonData['icons'][] = $request->icon_url_8;
            }
            // Store JSON data
            $request->merge(['content' => json_encode($jsonData)]);
            if ($request->hasFile('background_image')) {
                $background_image = uploadImageR2($request->file('background_image'), 'hero');
            }
            if ($request->hasFile('feature_image')) {
                $feature_image = uploadImageR2($request->file('feature_image'), 'hero');
            }
            $hero = Hero::find($id);
            $hero->update([
                'name' => 'Hero 1',
                'bg_image' => $background_image ?? $hero->bg_image,
                'image' => $feature_image ?? $hero->image,
                'content' => json_encode($jsonData),
            ]);
            toastr()->success('Hero updated successfully');
            return redirect()->back();
        } catch (\Exception $e) {
            toastr()->error('Hero updated failed');
            return redirect()->back();
        }
    }

    public function update2(Request $request, $id)
    {
        $request->validate([
            'title' => 'required|max:64',
            'pricing_plans' => 'required|array',
            'pricing_plans.*' => 'required|string',
            'background_image' => 'sometimes|image|mimes:jpeg,png,jpg,gif,webp,svg',
            'feature_image' => 'sometimes|image|mimes:jpeg,png,jpg,gif,webp,svg'
        ]);
        try {
            $jsonData = [
                'title' => $request->title,
                'pricing_plans' => $request->pricing_plans
            ];
            if ($request->hasFile('background_image')) {
                $background_image = uploadImageR2($request->file('background_image'), 'hero');
            }
            if ($request->hasFile('feature_image')) {
                $feature_image = uploadImageR2($request->file('feature_image'), 'hero');
            }
            $hero = Hero::find($id);
            $hero->update([
                'name' => 'Hero 2',
                'bg_image' => $background_image ?? $hero->bg_image,
                'image' => $feature_image ?? $hero->image,
                'content' => json_encode($jsonData),
            ]);
            toastr()->success('Hero updated successfully');
            return redirect()->back();
        } catch (\Exception $e) {
            dd($e);
            toastr()->error('Hero updated failed');
            return redirect()->back();
        }
    }

    public function update3(Request $request, $id)
    {
        $request->validate([
            'title' => 'required|max:64',
            'icon_url_1' => 'url',
            'icon_url_2' => 'url',
            'icon_url_3' => 'url',
            'icon_url_4' => 'url',
            'icon_url_5' => 'url',
            'icon_url_6' => 'url',
            'icon_url_7' => 'url',
            'icon_url_8' => 'url',
            'icon_url_9' => 'url',
            'icon_url_10' => 'url',
            'icon_url_11' => 'url',
            'icon_url_12' => 'url',
            'icon_url_13' => 'url',
            'icon_url_14' => 'url',
            'icon_url_15' => 'url',
            'icon_url_16' => 'url',
            'icon_url_17' => 'url',
            'icon_url_18' => 'url',
            'icon_url_19' => 'url',
            'icon_url_20' => 'url',
            'icon_url_21' => 'url',
            'icon_url_22' => 'url',
            'icon_url_23' => 'url',
            'icon_url_24' => 'url',
            'icon_url_25' => 'url',
            'icon_url_26' => 'url',
            'icon_url_27' => 'url',
            'icon_url_28' => 'url',
            'background_image' => 'sometimes|image|mimes:jpeg,png,jpg,gif,webp,svg',
        ]);
        try {
            $jsonData = [
                'title' => $request->title,
                'icons' => [
                    $request->icon_url_1,
                    $request->icon_url_2,
                    $request->icon_url_3,
                    $request->icon_url_4,
                    $request->icon_url_5,
                    $request->icon_url_6,
                    $request->icon_url_7,
                    $request->icon_url_8,
                    $request->icon_url_9,
                    $request->icon_url_10,
                    $request->icon_url_11,
                    $request->icon_url_12,
                    $request->icon_url_13,
                    $request->icon_url_14,
                    $request->icon_url_15,
                    $request->icon_url_16,
                    $request->icon_url_17,
                    $request->icon_url_18,
                    $request->icon_url_19,
                    $request->icon_url_20,
                    $request->icon_url_21,
                    $request->icon_url_22,
                    $request->icon_url_23,
                    $request->icon_url_24,
                    $request->icon_url_25,
                    $request->icon_url_26,
                    $request->icon_url_27,
                    $request->icon_url_28
                ]
            ];
            if ($request->hasFile('background_image')) {
                $background_image = uploadImageR2($request->file('background_image'), 'hero');
            }
            if ($request->hasFile('feature_image')) {
                $feature_image = uploadImageR2($request->file('feature_image'), 'hero');
            }
            $hero = Hero::find($id);
            $hero->update([
                'name' => 'Hero 3',
                'bg_image' => $background_image ?? $hero->bg_image,
                'image' => $feature_image ?? $hero->image,
                'content' => json_encode($jsonData),
            ]);

            toastr()->success('Hero updated successfully');
            return redirect()->back();
        } catch (\Exception $e) {
            toastr()->error('Hero updated failed');
            return redirect()->back();
        }
    }

    public function update4(Request $request, $id)
    {
        $request->validate([
            'title' => 'required|max:64',
            'button_url' => 'required|url',
            'background_image' => 'sometimes|image|mimes:jpeg,png,jpg,gif,webp,svg',
            'feature_image' => 'sometimes|image|mimes:jpeg,png,jpg,gif,webp,svg'
        ]);
        try {
            $jsonData = [
                'title' => $request->title,
                'button_url' => $request->button_url
            ];
            if ($request->hasFile('background_image')) {
                $background_image = uploadImageR2($request->file('background_image'), 'hero');
            }
            if ($request->hasFile('feature_image')) {
                $feature_image = uploadImageR2($request->file('feature_image'), 'hero');
            }
            $hero = Hero::find($id);
            $hero->update([
                'name' => 'Hero 4',
                'bg_image' => $background_image ?? $hero->bg_image,
                'image' => $feature_image ?? $hero->image,
                'content' => json_encode($jsonData),
            ]);

            toastr()->success('Hero updated successfully');
            return redirect()->back();
        } catch (\Exception $e) {
            toastr()->error('Hero updated failed');
            return redirect()->back();
        }
    }

    public function update5(Request $request, $id)
    {
        $request->validate([
            'title' => 'required|max:64',
            'feature_1' => 'required|max:64',
            'feature_2' => 'required|max:64',
            'feature_3' => 'required|max:64',
            'feature_4' => 'required|max:64',
            'feature_5' => 'required|max:64',
            'feature_6' => 'required|max:64',
            'feature_7' => 'required|max:64',
            'background_image' => 'sometimes|image|mimes:jpeg,png,jpg,gif,webp,svg',
            'feature_image' => 'sometimes|image|mimes:jpeg,png,jpg,gif,webp,svg'
        ]);

        try {
            $jsonData = [
                'title' => $request->title,
                'features' => [
                    $request->feature_1,
                    $request->feature_2,
                    $request->feature_3,
                    $request->feature_4,
                    $request->feature_5,
                    $request->feature_6,
                    $request->feature_7
                ]
            ];
            if ($request->hasFile('background_image')) {
                $background_image = uploadImageR2($request->file('background_image'), 'hero');
            }
            if ($request->hasFile('feature_image')) {
                $feature_image = uploadImageR2($request->file('feature_image'), 'hero');
            }
            $hero = Hero::find($id);
            $hero->update([
                'name' => 'Hero 5',
                'bg_image' => $background_image ?? $hero->bg_image,
                'image' => $feature_image ?? $hero->image,
                'content' => json_encode($jsonData),
            ]);

            toastr()->success('Hero updated successfully');
            return redirect()->back();
        } catch (\Exception $e) {
            toastr()->error('Hero updated failed');
            return redirect()->back();
        }
    }
}
