<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\DocumentFile;
use App\Models\DocumentFileDocument;
use App\Models\Document;
use App\Models\CourseType;
use App\Models\CourseDocument;
use App\Models\Course;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use App\Helper\Cache\ReviewsBlockCacheHelper;
use Illuminate\Support\Facades\Mail;
use App\Mail\NewsletterMail;
use App\Mail\ContactMail;

class TestController extends Controller
{
    public function index(){
        // $documentFile = new DocumentFile();
        // $documentFile->name = "name";
        // $documentFile->short_path = "short_path";
        // $documentFile->path = "path";
        // $documentFile->document_type = \App\Helper\Constant\DocumentTypeConstant::$pdf;
        // $documentFile->document_file_access = \App\Helper\Constant\DocumentFileAccess::$anyone;
        // $documentFile->save();

        // $document = new Document();
        // $document->title = "title";
        // $document->save();

        // $documentFileDocument = new DocumentFileDocument();
        // $documentFileDocument->document_id = 1;
        // $documentFileDocument->document_file_id = 1;
        // $documentFileDocument->save();

        // $document = Document::find(1);
        // $a = $document->documentFileDocuments;
        // foreach ($a as $b) {
        //     return $b->document_id;
        // }
        // return json_encode(count($a));
        // // $document->delete();
        //$documentFileDocument = DocumentFileDocument::find(1);
        //return $documentFileDocument->id;
        //return json_encode($documentFileDocument->documentFile);
        // $CourseType = new CourseType();
        // $CourseType->name = "CourseType";
        // $CourseType->save();

        // $Course = new Course();
        // $Course->course_type_id = $CourseType->id;
        // $Course->title = "title";
        // $Course->save();
        // $CourseDocument = new CourseDocument();
        // $CourseDocument->document_id = 1;
        // $CourseDocument->course_id = $Course->id;
        // $CourseDocument->save();
        // $document = Document::find(1);
        // $document->documentFiles()->sync([1]);
        //return json_encode(\App\Helper\Cache\FooterCacheHelper::getCacheFooter());
        //$footer = Cache::get(\App\Helper\Cache\FooterCacheHelper::$key, null);
        //$footer  = \App\Helper\Cache\FooterCacheHelper::getCacheFooter();
        //return $footer;
        //ReviewsBlockCacheHelper::cleanCacheReviewsBlocks();
        //$reviewsBlocks = ReviewsBlockCacheHelper::getCacheReviewsBlocks();
        //return json_encode($reviewsBlocks);
        //return $footer;


        //return "test";

        // $data = [
        //     'email' => 'huynh220592@gmail.com'
        // ];
        // Mail::to('huynh220592@gmail.com')->send(new NewsletterMail($data));

        $data = array(
            "fullName" => "phạm văn huỳnh",
            "phone" => "0368565419",
            "email" => "wormlazy19921@gmail.com",
            "address" => "chau can , phu xuyen, ha noi",
            "content" => "em muon dc tu van"
        );
        Mail::to('huynh220592@gmail.com')->send(new ContactMail($data));

        dd('Success! Email has been sent successfully.');
    }

    public function autocomplete(Request $request){

        $query = Document::query();
        if ($request->has("search")) {
            $query->where('title', 'LIKE', "%". $request->query("search")."%")
            ->orWhere('id', 'LIKE', "%". $request->query("search")."%");
        }

        $documents = $query->orderBy('id', 'desc')
        ->limit(10)
        ->get();
        $data = array();
        foreach ($documents as $document) {
            $item = array();
            $item["value"] = $document->id;
            $item["id"] = $document->id;
            $item["label"] = $document->title;
            array_push($data,$item);
        }
        return json_encode($data);
    }
}
