<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Repositories\Interfaces\BlogInterface;
use App\Traits\FileUploadTrait;
use Illuminate\Http\Request;

class BlogController extends Controller
{
    use FileUploadTrait;

    protected $blogRepo;
    private $blogImageCategoryId = 13; // must match creative_file_category

    public function __construct(BlogInterface $blogRepo)
    {
        $this->blogRepo = $blogRepo;
    }

    public function index(Request $request)
    {
        $blogs = $this->blogRepo->list($request);
        $baseImgPath = $this->getBasePath();
        return view('Admin.Blog.list', compact('blogs', 'baseImgPath'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'name_en'           => 'required|string|max:255',
            'author_name'       => 'nullable|string|max:255',
            'short_description' => 'nullable|string',
            'long_description'  => 'nullable|string',
            'thumb_image'       => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
            'large_image'       => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
            'author_image'      => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
            'status'            => 'nullable|in:0,1'
        ]);

        $data = $request->except('thumb_image', 'large_image', 'author_image', '_token');

        // Upload images with progress tracking (we'll handle progress in JS)
        if ($request->hasFile('thumb_image')) {
            $data['thumb_image'] = $this->uploadFile($this->blogImageCategoryId, $request->file('thumb_image'));
        }
        if ($request->hasFile('large_image')) {
            $data['large_image'] = $this->uploadFile($this->blogImageCategoryId, $request->file('large_image'));
        }
        if ($request->hasFile('author_image')) {
            $data['author_image'] = $this->uploadFile($this->blogImageCategoryId, $request->file('author_image'));
        }

        $data['status'] = $request->status ?? 1;

        $blog = $this->blogRepo->store($data);
        return response()->json(['success' => true, 'blog' => $blog]);
    }

    public function edit($id)
    {
        $blog = $this->blogRepo->find($id);
        if (!$blog) {
            return response()->json(['success' => false, 'message' => 'Blog not found'], 404);
        }
        $blog->thumb_image_url = $blog->thumb_image ? $this->getBasePath() . $blog->thumb_image : null;
        $blog->large_image_url = $blog->large_image ? $this->getBasePath() . $blog->large_image : null;
        $blog->author_image_url = $blog->author_image ? $this->getBasePath() . $blog->author_image : null;
        return response()->json(['success' => true, 'blog' => $blog]);
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'name_en'           => 'required|string|max:255',
            'author_name'       => 'nullable|string|max:255',
            'short_description' => 'nullable|string',
            'long_description'  => 'nullable|string',
            'thumb_image'       => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
            'large_image'       => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
            'author_image'      => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
            'status'            => 'nullable|in:0,1'
        ]);

        $data = $request->except('thumb_image', 'large_image', 'author_image', '_token', '_method');

        if ($request->hasFile('thumb_image')) {
            $data['thumb_image'] = $this->uploadFile($this->blogImageCategoryId, $request->file('thumb_image'));
        }
        if ($request->hasFile('large_image')) {
            $data['large_image'] = $this->uploadFile($this->blogImageCategoryId, $request->file('large_image'));
        }
        if ($request->hasFile('author_image')) {
            $data['author_image'] = $this->uploadFile($this->blogImageCategoryId, $request->file('author_image'));
        }

        $blog = $this->blogRepo->update($id, $data);
        return response()->json(['success' => true, 'blog' => $blog]);
    }

    public function destroy($id)
    {
        $result = $this->blogRepo->delete($id);
        if ($result) {
            return response()->json(['success' => true]);
        }
        return response()->json(['success' => false, 'message' => 'Blog not found'], 404);
    }
}
