<?php

namespace App\Http\Controllers\Dashboard\PaymentSystem;

use App\Http\Controllers\Controller as ExController;
use App\Models\PaymentSystemItem;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class PaymentSystemItemController extends ExController
{
    public function index($paymentSystemId)
    {
        $items = PaymentSystemItem::latest('id')->where('payment_system_id', $paymentSystemId)->get();

        return view('dashboard.payment-systems.items.index', compact('items', 'paymentSystemId'));
    }

    public function create($paymentSystemId)
    {
        return view('dashboard.payment-systems.items.create', compact('paymentSystemId'));
    }

    public function store(Request $request, $paymentSystemId)
    {
        $this->validate($request, [
            'title_uz' => 'required|string',
            'title_ru' => 'required|string',
            'description_uz' => 'nullable|string',
            'description_ru' => 'nullable|string',
            'logo' => 'required|image',
        ]);

        // store logo to storage
        $logo = $request->file('logo');
        if (env('FILESYSTEM_DISK') == 's3') {
            $folder = "uploads/payment-system-items";
            $logoPath = $logo->store($folder);
        } else {
            $logoName = Str::random(20) . '.' . $logo->extension();
            $logo->storeAs('uploads/payment-system-items', $logoName);
            $logoPath = 'uploads/payment-system-items/' . $logoName;
        }

        // generate slug from title_uz
        $slug = Str::slug($request->title_uz);

        if ($request->has('description_uz') && $request->has('description_ru')) {
            $description = [
                'uz' => $request->description_uz,
                'ru' => $request->description_ru,
            ];
        } else {
            $description = null;
        }

        PaymentSystemItem::create([
            'title' => [
                'uz' => $request->title_uz,
                'ru' => $request->title_ru,
            ],
            'payment_system_id' => $paymentSystemId,
            'description' => $description,
            'slug' => $slug,
            'logo' => $logoPath
        ]);

        $this->info(trans('admin.messages.created'));
        return redirect()->route('dashboard.payment-system-items.index', $paymentSystemId);
    }

    public function edit($paymentSystemId, $systemId)
    {

        $item = PaymentSystemItem::findOrFail($systemId);
        return view('dashboard.payment-systems.items.edit', compact('item', 'paymentSystemId'));
    }

    public function update(Request $request, $paymentSystemId, $systemId)
    {
        $item = PaymentSystemItem::findOrFail($systemId);

        $this->validate($request, [
            'title_uz' => 'required|string',
            'title_ru' => 'required|string',
            'description_uz' => 'nullable|string',
            'description_ru' => 'nullable|string',
            'logo' => 'nullable|image',
        ]);

        // generate slug from title_uz
        $slug = Str::slug($request->title_uz);

        if ($request->has('description_uz') && $request->has('description_ru')) {
            $description = [
                'uz' => $request->description_uz,
                'ru' => $request->description_ru,
            ];
        } else {
            $description = null;
        }

        if ($request->hasFile('logo')) {
            // delete old logo
            Storage::delete($item->logo);

            // store logo to storage
            $logo = $request->file('logo');
            // with generated name
            // generate name with 20 string
            $logoName = Str::random(20) . '.' . $logo->extension();
            $logo->storeAs('uploads/payment-system-items', $logoName);

            $logo = 'uploads/payment-system-items/' . $logoName;
        } else {
            $logo = $item->logo;
        }

        $item->update([
            'title' => [
                'uz' => $request->title_uz,
                'ru' => $request->title_ru,
            ],
            'payment_system_id' => $paymentSystemId,
            'description' => $description,
            'slug' => $slug,
            'logo' => $logo,
        ]);

        $this->info(trans('admin.messages.updated'));
        return redirect()->route('dashboard.payment-system-items.index', $paymentSystemId);
    }

    public function destroy($paymentSystemId, $systemId)
    {
        $item = PaymentSystemItem::findOrFail($systemId);

        Storage::delete($item->logo);

        $item->delete();

        $this->info(trans('admin.messages.deleted'));
        return redirect()->back();
    }
}
