<?php

namespace Modules\Admin\app\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Banner;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Modules\Admin\app\Http\Requests\BannerRequest;

class BannerController extends Controller
{
    public function all(Request $request)
    {
        $package = new Banner;

        return response()->json([
            'data' => $package->get(),
            'status' => true,
        ]);
    }

    public function create(BannerRequest $request)
    {
        
        $banner = new Banner;

        if ($request->has('title')) {
            $banner->title = $request->get('title');
        }
        if ($request->has('link')) {
            $banner->link = $request->get('link');
        }
        if ($request->has('is_active')) {
            $banner->is_active = in_array(
                needle: $request->get('is_active'),
                haystack: ['false', '0']
            ) ? 0 : 1;
        }
        $banner->save();

        if ($request->hasFile('image')) {
            $file = $request->file('image');
            $filename = $banner->id . '/' . Str::random(20) . '.' . $file->getClientOriginalExtension();
            $path = Banner::STORAGE . "/$filename";

            Storage::disk('public')->put(
                path: $path,
                contents: $file->get()
            );

            $banner->image = $path;
            $banner->save();
        }

        return response()->json([
            'data' => $banner,
            'status' => true
        ]);
    }

    public function update(BannerRequest $request)
    {
        $id = $request->get('id');

        $banner = Banner::find($id);

        if ($request->has('title')) {
            $banner->title = $request->get('title');
        }
        if ($request->has('link')) {
            $banner->link = $request->get('link');
        }
        if ($request->has('is_active')) {
            $banner->is_active = in_array(
                needle: $request->get('is_active'),
                haystack: ['false', '0']
            ) ? 0 : 1;
        }
        $banner->save();

        if ($request->hasFile('image')) {
            $file = $request->file('image');
            $filename = $banner->id . '/' . Str::random(20) . '.' . $file->getClientOriginalExtension();
            $path = Banner::STORAGE . "/$filename";

            // delete old file
            if (Storage::disk('public')->exists($banner->getRawOriginal('image'))) {
                Storage::disk('public')->delete($banner->getRawOriginal('image'));
            }
            // put a new file
            Storage::disk('public')->put(
                path: $path,
                contents: $file->get()
            );
            $banner->image = $path;
            $banner->save();
        }

        return response()->json([
            'data' => $banner,
            'status' => true
        ]);
    }

    public function delete(BannerRequest $request)
    {
        $id = $request->get('id');

        Banner::destroy($id);
        return response()->json([
            'status' => true
        ]);
    }

    // Delete multiple packages
    public function deletes(BannerRequest $request)
    {
        $banners = $request->get('banners');
        $ids = collect($banners)->pluck('id');
        Banner::whereIn('id', $ids)->delete();

        return response()->json([
            'data' => $ids,
            'status' => true
        ]);
    }

    // Update multiple banners
    public function updates(BannerRequest $request)
    {
        $banners = $request->get('banners');
        $ids = collect($banners)->pluck('id');

        foreach ($banners as $bannersRequest) {
            // convert to object|array to array
            $bannersRequest = collect($bannersRequest)->toArray();
            // handle array
            $package = Package::find($bannersRequest['id']);
            if ($package) {
                // exclude id field
                unset($package['id']);
                $package->update($bannersRequest);
            }
        }

        return response()->json([
            'data' => Package::whereIn('id', $ids)->get(),
            'status' => true
        ]);
    }
}
