<?php

namespace App\Http\Controllers;

use App\Models\Mapping;
use App\Models\PlatformMapping;
use Illuminate\Http\Request;

class MappingController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public $allPlatforms = [
        'foodics',
    ];
    public function index()
    {
        $data['mappings'] = Mapping::orderBy('mappings.id', 'desc')->paginate(10);

        return view('mappings.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $availablePlatforms = $this->allPlatforms;
        return view('mappings.create', compact('availablePlatforms'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        // dd($request->all());
        $request->validate([
            'type' => 'required|unique:mappings,type',
        ]);
        $availablePlatforms = $this->allPlatforms;
        // create input array w.r.t platforms
        $input = [
            'type' => $request->type,
        ];
        foreach ($request->all() as $key => $value) {
            $arr = explode('|', $key);
            if (isset($arr[2])) {
                $input['data'][$arr[0]][$arr[1]][$arr[2]] = $value;
                //$arr[0]=platform, $arr[1]=>step num, $arr[2]=>param
            }
        }
        // dd($input);

        $mapping = Mapping::create([
            'type' => $input['type'],
        ]);
        foreach ($input['data'] as $platform => $step_arr) {
            // dd($step_arr);
            foreach ($step_arr as $step_num => $step) {
                $step['dependent'] = isset($step['dependent']) ? 1 : 0;
                $step['repetitive'] = isset($step['repetitive']) ? 1 : 0;
                $step['platform'] = $platform;
                $step['mapping_id'] = $mapping->id;
                PlatformMapping::create($step);
            }
        }

        return redirect()->route('mappings.index')
            ->with('success', 'Mapping has been created successfully.');
    }

    /**
     * Display the specified resource.
     */
    // public function show(string $id)
    // {
    //     return view('mappings.show', compact('mapping'));
    // }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        $availablePlatforms = $this->allPlatforms;
        $mappings = Mapping::join('platform_mappings', 'platform_mappings.mapping_id', '=', 'mappings.id')
            ->where('mappings.id', $id)
            ->groupBy('platform')
            ->select('mappings.id', 'mappings.type', 'platform_mappings.platform')
            ->get();
        foreach ($mappings as $platform_mapping) {
            $platform_mapping->steps = PlatformMapping::where('mapping_id', $id)
                ->where('platform', $platform_mapping->platform)
                ->orderBy('step')
                ->get();
        }
        if (!isset($mappings[0])) {
            return redirect('mappings')->with('msg', 'Mappings for platform is not added ');
        }
        return view('mappings.edit', compact('mappings', 'availablePlatforms'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        // dd($request->all());
        $request->validate([
            'type' => 'required',
        ]);
        $availablePlatforms = $this->allPlatforms;
        // create input array w.r.t platforms
        $input = [
            'type' => $request->type,
        ];

        foreach ($request->all() as $key => $value) {
            $arr = explode('|', $key);
            if (isset($arr[2])) {
                $input['data'][$arr[0]][$arr[1]][$arr[2]] = $value;
                //$arr[0]=platform, $arr[1]=>step num, $arr[2]=>param
            }
        }
        // dd($input);

        $mapping = Mapping::find($id);
        PlatformMapping::where('mapping_id', $id)->delete();

        foreach ($input['data'] as $platform => $step_arr) {
            // dd($step_arr);
            foreach ($step_arr as $step_num => $step) {
                $step['dependent'] = isset($step['dependent']) ? 1 : 0;
                $step['repetitive'] = isset($step['repetitive']) ? 1 : 0;
                $step['platform'] = $platform;
                $step['mapping_id'] = $id;
                PlatformMapping::create($step);
            }
        }
        unset($input['data']);
        $mapping = Mapping::where('id', $id)->update($input);

        return redirect()->route('mappings.index')
            ->with('success', 'Mapping Has Been updated successfully');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        PlatformMapping::where('mapping_id', $id)->delete();
        $mapping = Mapping::find($id);
        $mapping->delete();

        return redirect()->route('mappings.index')
            ->with('success', 'Mapping has been deleted successfully');
    }
}
