<?php

namespace Modules\Advertisement\Http\Controllers;

use App\Functions\Upload;
use App\Http\Controllers\BasicController;
use Illuminate\Http\Request;
use Modules\Advertisement\Entities\Model;
use Modules\Category\Entities\Model as Category;
use Modules\Product\Entities\Model as Product;

class Controller extends BasicController
{
    public function index(Request $request)
    {
        $Models = Model::with('linkable')->get();

        return view('advertisement::index', compact('Models'));
    }

    public function create()
    {
        $Categories = $this->renderCategoryOptions();
        $Products = Product::all();

        return view('advertisement::create', compact('Categories', 'Products'));
    }

    public function store(Request $request)
    {
        $Model = new Model;
        $linkableType = null;
        $linkableId = null;

        if ($request->type == 'product') {
            $linkableType = Product::class;
            $linkableId = $request->product;
        } elseif ($request->type == 'category') {
            $linkableType = Category::class;
            $linkableId = $request->category;
        } else {
            alert()->error(__('trans.invalidType'));

            return redirect()->back();
        }

        if ($request->hasFile('file')) {
            $Model->file = Upload::UploadFile($request->file, 'Advertisements');

        }

        $Model->title_ar = $request->title_ar;
        $Model->title_en = $request->title_en;
        $Model->linkable_type = $linkableType;
        $Model->linkable_id = $linkableId;
        $Model->status = $request->get('status', 1);
        $Model->save();

        alert()->success(__('trans.addedSuccessfully'));

        return redirect()->back();
    }

    public function show($id)
    {
        $Model = Model::where('id', $id)->firstorfail();

        return view('advertisement::show', compact('Model'));
    }

    public function edit($id)
    {
        $Model = Model::where('id', $id)->firstorfail();
        $Categories = $this->renderCategoryOptions();
        $product_id = -1;
        if ($Model->linkable_type == Category::class) {
            $Categories = $this->renderCategoryOptions([$Model->linkable_id]);
        } elseif ($Model->linkable_type == Product::class) {
            $product_id = $Model->linkable_id;
        }

        $Products = Product::all();

        return view('advertisement::edit', compact('Model', 'Categories', 'Products', 'product_id'));
    }

    public function update(Request $request, $id)
    {

        $Model = Model::where('id', $id)->firstorfail();
        $linkableType = null;
        $linkableId = null;

        if ($request->type == 'product') {
            $linkableType = Product::class;
            $linkableId = $request->product;
        } elseif ($request->type == 'category') {
            $linkableType = Category::class;
            $linkableId = $request->category;
        } else {
            alert()->error(__('trans.invalidType'));

            return redirect()->back();
        }

        if ($request->hasFile('file')) {

            $Model->file = Upload::UploadFile($request->file, 'Advertisements');
            $Model->save();
        }

        $Model->title_ar = $request->title_ar;
        $Model->title_en = $request->title_en;
        $Model->linkable_type = $linkableType;
        $Model->linkable_id = $linkableId;
        $Model->status = $request->get('status', 1);
        $Model->save();

        alert()->success(__('trans.updatedSuccessfully'));

        return redirect()->back();
    }

    public function destroy($id)
    {
        $Model = Model::where('id', $id)->delete();
    }

    public function renderCategoryOptions($selectedCategoryIds = [], $path = '', $Categories = null)
    {
        if ($Categories === null) {
            $Categories = Category::whereNull('parent_id')->orWhere('parent_id', 0)->get();
        }

        $html = '';
        foreach ($Categories as $Category) {
            $currentPath = $path ? $path.' - '.$Category->title() : $Category->title();

            if ($Category->children && $Category->children->isNotEmpty()) {
                $html .= $this->renderCategoryOptions($selectedCategoryIds, $currentPath, $Category->children);
            } else {
                $isSelected = in_array($Category->id, $selectedCategoryIds) ? 'selected' : '';
                $html .= '<option value="'.$Category->id.'" '.$isSelected.'>'.$currentPath.'</option>';
            }
        }

        return $html;
    }
}
