<?php

namespace App\Services\Admin;

use App\Models\Post;
use App\Services\BaseService;
use Illuminate\Support\Facades\Auth;
use Yajra\Datatables\Datatables;
use Illuminate\Support\Str;
use App\Models\TempPublicImage;
use File;

class PostService extends BaseService
{
    private $uploadFileService;

    public function __construct()
    {
        $this->uploadFileService = resolve(UploadFileService::class);
    }

    public function uploadPostFile($uploadedFile)
    {
        return $this->uploadFileService->uploadPublicFile($uploadedFile, 'post', 'posts');
    }

    public function uploadPostFiles($uploadedFiles)
    {
        return $this->uploadFileService->uploadPublicFiles($uploadedFiles, 'post', 'posts');
    }

    public function deleteUploadedPostFile($data)
    {
        if ($data['image_id'] && $data['image_id'] !== '') {
            $tempPublicImageId = $data['image_id'];
            if (!empty($tempPublicImageId)) {
                $tempPublicImage = TempPublicImage::find($tempPublicImageId);
                if ($tempPublicImage) {
                    if (File::exists($tempPublicImage->absolute_path)) {
                        $tempPublicImage->delete();
                        File::delete($tempPublicImage->absolute_path);
                    }
                }
            }
        }
        if ($data['image_path'] && $data['image_path'] !== '') {
            $imagePath = $data['image_path'];
            if (!empty($imagePath)) {
                if (File::exists($imagePath)) {
                    $filename = explode('/', $imagePath);
                    $check1 = Post::where('thumb_image', $data['image_path'])->first();
                    if ($check1)
                        $check1->update(['thumb_image' => null]);
                    File::delete($imagePath);
                }
            }
        }
    }
    
    public function postsList()
    {
        $usersList = Post::with('category');
        $datatable = Datatables::of($usersList);
        return collect($datatable->make(true)->getData());
    }

    public function getPostDetail($id)
    {
        return Post::findOrFail($id);
    }

    public function createPost(array $data)
    {
        $module = new Post();
        $module->category_id = $data['category_id'] ?? null;
        $module->title = $data['title'] ?? null;
        $module->slug = Str::slug($data['title'], '-');
        $module->full_desc = $data['full_desc'] ?? null;
        $module->meta_title = $data['meta_title'] ?? null;
        $module->meta_keywords = $data['meta_keywords'] ?? null;
        $module->meta_desc = $data['meta_desc'] ?? null;
        $module->added_by = Auth::id();
        if (isset($data['thumb_image_id']) && $data['thumb_image_id']) {
            $image = NULL;
            $tempPublicImageId = $data['thumb_image_id'];
            $tempPublicImage = TempPublicImage::find($tempPublicImageId);
            if ($tempPublicImage) {
                if (File::exists($tempPublicImage->path)) {
                    $image = $tempPublicImage->path;
                }
                $tempPublicImage->delete();
            }
            $module->thumb_image = $image;
        }
        $module->save();
    }

    public function updatePost($data, $id)
    {
        $module = Post::findOrFail($id);
        $module->category_id = $data['category_id'] ?? null;
        $module->title = $data['title'] ?? null;
        $module->slug = Str::slug($data['title'], '-');
        $module->full_desc = $data['full_desc'] ?? null;
        $module->meta_title = $data['meta_title'] ?? null;
        $module->meta_keywords = $data['meta_keywords'] ?? null;
        $module->meta_desc = $data['meta_desc'] ?? null;
        $module->updated_by = Auth::id();
        if (isset($data['thumb_image_id']) && $data['thumb_image_id'] !== '') {
            $timage = '';
            $tempPublicImageId = $data['thumb_image_id'];
            $tempPublicImage = TempPublicImage::find($tempPublicImageId);
            if ($tempPublicImage) {
                if (File::exists($tempPublicImage->path)) {
                    $timage = $tempPublicImage->path;
                }
                $tempPublicImage->delete();
            }
            $module->thumb_image = $timage;
        }
        $module->save();
    }
}
