<?php

namespace App\Http\Controllers\Admin\Api;

use App\Http\Controllers\Controller;
use App\Models\Banner;
use App\libs\Response\GlobalApiResponse;
use App\libs\Response\GlobalApiResponseCodeBook;
use App\Services\Admin\BannerService;
use Illuminate\Http\Request;
use File;

class BannersController extends Controller
{
    private $bannerService;

    public function __construct(BannerService $bannerService) {
        $this->bannerService = $bannerService;
    }

    public function bannersList(){
        $data = $this->bannerService->bannersList();
        return (new GlobalApiResponse())->success('Banners list',1,$data);
    }

    public function getBannerDetail($id){
        $data = $this->bannerService->getBannerDetail($id);
        return (new GlobalApiResponse())->success('Banners Details',1,$data);
    }

    public function createBanner(Request $request){
        $this->bannerService->createBanner($request->all());
        if($this->bannerService->hasError()) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, $this->bannerService->getErrors()[0]['message'], $this->bannerService->getErrors());
        }
        return (new GlobalApiResponse())->success('Banner successfully created');
    }

    public function updateBanner(Request $request, $id){
        $this->bannerService->updateBanner($request->all(), $id);
        if($this->bannerService->hasError()) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, $this->bannerService->getErrors()[0]['message'], $this->bannerService->getErrors());
        }
        return (new GlobalApiResponse())->success('Banner successfully updated');
    }

    public function deleteBanner(Request $request){
        if (empty($request->banner_ids)) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, "IDs not found!");
        }
        $mods = Banner::whereIn('id', $request->banner_ids)->get();
        if (!empty($mods)) {
            foreach ($mods as $mod) {
                $mod->delete();
                if (File::exists($mod->thumb_image) && $mod->thumb_image !== 'public/images/banner-placeholder.jpg') {
                    File::delete($mod->thumb_image);
                }
            }
            return (new GlobalApiResponse())->success("Banner successfully deleted");
        } else {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::RECORD_NOT_EXIST, "Banner not found!");
        }
    }

    public function uploadBannerFile(Request $request)
    {
        $tempPublicImage = $this->bannerService->uploadBannerFile($request->file('file'));
        return (new GlobalApiResponse())->success('File successfully saved', 1, $tempPublicImage);
    }

    public function uploadBannerFiles(Request $request)
    {
        $tempPublicImage = $this->bannerService->uploadBannerFiles($request->file('file'));
        return (new GlobalApiResponse())->success('File successfully saved', 1, $tempPublicImage);
    }

    public function deleteUploadedBannerFile(Request $request)
    {
        $this->bannerService->deleteUploadedBannerFile($request->all());
        return (new GlobalApiResponse())->success('File successfully deleted', 1);
    }

    public function updateBannerStatus(Request $request)
    {
        if (empty($request->banner_ids)) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, "IDs not found!");
        }
        $mods = Banner::whereIn('id', $request->banner_ids)->get();
        if ($mods) {
            foreach ($mods as $mod) {
                $mod->update([$request->column => $request->status]);
            }
            return (new GlobalApiResponse())->success("Status successfully updated");
        } else {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::RECORD_NOT_EXIST, "Banner not found!");
        }
    }
}
