<?php

namespace App\Http\Controllers\Admin\Api;

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

class AttributesController extends Controller
{
    private $attributeService;

    public function __construct(AttributeService $attributeService) {
        $this->attributeService = $attributeService;
    }

    public function attributesList(Request $request){
        $data = $this->attributeService->attributesList($request);
        return (new GlobalApiResponse())->success('Attributes list',1,$data);
    }

    public function getAttributeDetail($id){
        $data = $this->attributeService->getAttributeDetail($id);
        return (new GlobalApiResponse())->success('Attributes Details',1,$data);
    }

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

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

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

    public function createAttribute(Request $request){
        $this->attributeService->createAttribute($request->all());
        if($this->attributeService->hasError()) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_CREDENTIALS, 'Oops, an error occurred', $this->productService->getErrors());
        }
        return (new GlobalApiResponse())->success('Created Successfully');
    }

    public function updateAttribute(Request $request, $id){
        $this->attributeService->updateAttribute($request->all(), $id);
        if($this->attributeService->hasError()) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_CREDENTIALS, 'Oops, an error occurred', $this->productService->getErrors());
        }
        return (new GlobalApiResponse())->success('Updated Successfully');
    }

    public function deleteAttribute(Request $request){
        if (empty($request->attribute_ids)) {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::INVALID_FORM_INPUTS, "IDs not found!");
        }
        $attributes = Attribute::whereIn('id', $request->attribute_ids)->get();
        if (!empty($attributes)) {
            foreach ($attributes as $attribute) {
                $attribute->delete();
                if (File::exists($attribute->thumb_image) && $attribute->thumb_image !== 'public/images/banner-placeholder.jpg') {
                    File::delete($attribute->thumb_image);
                }
            }
            return (new GlobalApiResponse())->success("Deleted Successfully");
        } else {
            return (new GlobalApiResponse())->error(GlobalApiResponseCodeBook::RECORD_NOT_EXIST, "Attribute not found!");
        }
    }

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