<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Models\Folder;
use App\Models\FolderArt;
use App\Models\Law;
use App\Traits\BaseApiResponse;
use Illuminate\Http\Request;

class FolderController extends Controller
{
    use BaseApiResponse;

    public function index()
    {
        $folder = Folder::query()->where('user_id', auth()->id())->get()
            ->map(function ($q) {
                return [
                    "id" => $q->id,
                    "name" => $q->name,
                    "count_of_arts" => $q->arts->count(),
                    "created_at" => $q->created_at,
                ];
            });


        return $this->success($folder);
    }

    public function create(Request $request)
    {
        $validted = $request->validate([
            'name' => 'required'
        ]);

        Folder::create([
            'user_id' => auth()->user()->id,
            'name' => $validted['name']
        ]);

        return $this->success([], 'ایجاد شد', 'پوشه با موفقیت ایجاد شد.');
    }

    public function assign(Request $request)
    {
        $validted = $request->validate([
            'folder_id' => 'required',
            'art_id' => 'required'
        ]);

        if (Folder::query()->where('id', $validted['folder_id'])->count() == 0) {
            return $this->failed([], 'پوشه مورد نظر یافت نشد', 404);
        }

        if (FolderArt::query()->where('folder_id', $validted['folder_id'])->where('art_id', $validted['art_id'])->count() > 0) {
            return $this->failed([], 'ارت مورد نظر قبلا اضافه شده است', 400);
        }

        FolderArt::create([
            'folder_id' => $validted['folder_id'],
            'art_id' => $validted['art_id']
        ]);

        return $this->success([], 'اضافه شد', 'ارت با موفیت اضافه شد');
    }

    public function folder($id)
    {
        $arts = FolderArt::where('folder_id', $id)
            ->with('arts.chapter', 'arts.part', 'arts.volum', 'arts.law', 'arts.book', 'arts.section', 'arts.gate')
            ->get()
            ->pluck('arts')
            ->flatten();

        $arts = $arts->map(function ($art) {
            $text = $art->text ?? '';
            $shortText = '';
            if (strlen($text) > 50) {
                $shortText = substr($text, 0, 50);
                $shortText = substr($shortText, 0, strrpos($shortText, ' ')) . '...';
            } else {
                $shortText = $text;
            }
            
            return [
                'id' => $art->id,
                'title' => $art->title,
                'text' => $shortText,
                'number' => $art->number,
                'is_locked' => auth()->user()->isSubscriber()  !== false ? false : Law::where('is_locked', $art->law->id)->first()?->is_locked,
                'chapter' => $art->chapter != null ? [
                    'id' => $art->chapter->id,
                    'title' => $art->chapter->title,
                    'number' => $art->chapter->number,
                ] : null,
                'part' => $art->part != null ? [
                    'id' => $art->part->id,
                    'title' => $art->part->title,
                    'number' => $art->part->number,
                ] : null,
                'volum' => $art->volum != null ? [
                    'id' => $art->volum->id,
                    'title' => $art->volum->title,
                    'number' => $art->volum->number,
                ] : null,
                'law' => $art->law != null ? [
                    'id' => $art->law->id,
                    'title' => $art->law->title,
                    'number' => $art->law->number,
                ] : null,
                'book' => $art->book != null ? [
                    'id' => $art->book->id,
                    'title' => $art->book->title,
                    'number' => $art->book->number,
                ] : null,
                'section' => $art->section != null ? [
                    'id' => $art->section->id,
                    'title' => $art->section->title,
                    'number' => $art->section->number,
                ] : null,
                'gate' => $art->gate != null ? [
                    'id' => $art->gate->id,
                    'title' => $art->gate->title,
                    'number' => $art->gate->number,
                ] : null,
            ];
        });

        return $this->success($arts);
    }

    public function delete_folder($id)
    {
        if (Folder::query()->where('id', $id)->count() == 0) {
            return $this->failed('پوشه یافت نشد', 'پوشه مورد نظر یافت نشد', 404);
        }
        Folder::query()->where('id', $id)->delete();
        return $this->success([], 'حذف شد', 'پوشه با موفقیت حذف شد');
    }

    public function delete_art($id,$art_id)
    {
        if (FolderArt::query()->where('folder_id', $id)->where('art_id', $art_id)->count() == 0) {
            return $this->failed([], 'ارت مورد نظر یافت نشد', 404);
        }
        FolderArt::query()->where('folder_id', $id)->where('art_id', $art_id)->delete();
        return $this->success([], 'حذف شد', 'ارت با موفقیت حذف شد');
    }
}
