<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\ArticleResource;
use App\Models\Article;
use DB;
use Illuminate\Http\Request;
use Storage;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $article = Article::where(function ($query) use ($request) {
            if ($request->category)
                $query->where('category', '=', $request->category);
        })->where(function ($query) use ($request) {
            if ($request->is_visible)
                $query->where('is_visible', true)->get();
        })->where(function ($query) use ($request) {
            if ($request->is_highlight)
                $query->where('is_highlight', true)->get();
        })->latest()->get();

        $data = [];

        foreach ($article as $v) {
            $tags = DB::table('articles')
                ->join('taggables', 'articles.id', '=', 'taggables.taggable_id')
                ->join('tags', 'taggables.tag_id', '=', 'tags.id')
                ->where('taggables.taggable_id', $v->id)
                ->select('tags.name')
                ->get();

            $data[] = [
                'id' => $v->id,
                'thumbnail' => Storage::disk('public')->url($v->thumbnail),
                'title' => $v->title,
                'slug' => $v->slug,
                'category' => $v->category,
                'preview' => $v->preview,
                'body' => $v->body,
                'is_highlight' => $v->is_highlight,
                'is_visible' => $v->is_visible,
                'published_at' => $v->published_at->format('Y-m-d H:i:s'),
                'updated_by' => $v->updated_by,
                'created_by' => $v->created_by,
                'tags' => $tags->pluck('name')->map(function ($tag) {
                    $decodedTag = json_decode($tag, true); // Decode each JSON string to an array
                    return $decodedTag['en']; // Extract the 'en' value
                }),
            ];
        }

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

    public function homeArticle(Request $request)
    {
        $article = Article::where(function ($query) use ($request) {
            if ($request->category)
                $query->where('category', '=', $request->category);
        })->where(function ($query) use ($request) {
            if ($request->is_visible)
                $query->where('is_visible', true)->get();
        })->where(function ($query) use ($request) {
            if ($request->is_highlight)
                $query->where('is_highlight', true)->get();
        })->latest()->limit(5)->get();

        $data = [];

        foreach ($article as $v) {
            $tags = DB::table('articles')
                ->join('taggables', 'articles.id', '=', 'taggables.taggable_id')
                ->join('tags', 'taggables.tag_id', '=', 'tags.id')
                ->where('taggables.taggable_id', $v->id)
                ->select('tags.name')
                ->get();

            $data[] = [
                'id' => $v->id,
                'thumbnail' => Storage::disk('public')->url($v->thumbnail),
                'title' => $v->title,
                'slug' => $v->slug,
                'category' => $v->category,
                'preview' => $v->preview,                
                'is_highlight' => $v->is_highlight,
                'is_visible' => $v->is_visible,
                'published_at' => $v->published_at->format('Y-m-d H:i:s'),
                'updated_by' => $v->updated_by,
                'created_by' => $v->created_by,
                'tags' => $tags->pluck('name')->map(function ($tag) {
                    $decodedTag = json_decode($tag, true); // Decode each JSON string to an array
                    return $decodedTag['en']; // Extract the 'en' value
                }),
            ];
        }

        return response()->json([
            'data' => $data,
        ]);

    }

    public function show($slug)
    {
        try {
            $article = Article::where('slug', $slug)->get();
            if (!$article) {
                abort(404);
            }

            foreach ($article as $v) {
                $tags = DB::table('articles')
                    ->join('taggables', 'articles.id', '=', 'taggables.taggable_id')
                    ->join('tags', 'taggables.tag_id', '=', 'tags.id')
                    ->where('taggables.taggable_id', $v->id)
                    ->select('tags.name')
                    ->get();

                $data[] = [
                    'id' => $v->id,
                    'thumbnail' => Storage::disk('public')->url($v->thumbnail),
                    'title' => $v->title,
                    'slug' => $v->slug,
                    'category' => $v->category,
                    'preview' => $v->preview,
                    'body' => $v->body,
                    'is_highlight' => $v->is_highlight,
                    'is_visible' => $v->is_visible,
                    'published_at' => $v->published_at->format('Y-m-d H:i:s'),
                    'updated_by' => $v->updated_by,
                    'created_by' => $v->created_by,
                    'tags' => $tags->pluck('name')->map(function ($tag) {
                        $decodedTag = json_decode($tag, true); // Decode each JSON string to an array
                        return $decodedTag['en']; // Extract the 'en' value
                    }),
                ];
            }

            return response()->json([
                'data' => $data,
            ]);
        } catch (\Exception $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }
}