<?php

namespace App\Http\Controllers\Admin;

use App\Exports\CertificatesExport;
use App\Http\Controllers\Controller;
use App\Mixins\Certificate\MakeCertificate;
use App\Models\Certificate;
use App\Models\CertificateTemplateIMT;
use App\Models\QuizzesResult;
use App\Models\Translation\CertificateTemplateTranslation;
use App\Services\UserSerialGenerator;
use App\User;
use App\Models\Quiz;
use App\Models\CertificateTemplate;
use Intervention\Image\Facades\Image;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Str;


class CertificateController extends Controller
{
    public function index(Request $request)
    {
        $this->authorize('admin_certificate_list');

        $query = Certificate::whereNull('webinar_id');

        $query = $this->filters($query, $request);

        $certificates = $query->with(
            [
                'quiz' => function ($query) {
                    $query->with(['webinar', 'teacher']);
                },
                'student',
                'quizzesResult'
            ]
        )->orderBy('created_at', 'desc')
            ->paginate(10);

        $filters = $request->all();
        $data = [
            'pageTitle' => trans('admin/main.certificate_list_page_title'),
            'certificates' => $certificates,
            'student' => $filters['student'] ?? null,
            'instructor' => $filters['instructor'] ?? null,
            'quiz_title' => $filters['quiz_title'] ?? null,
        ];

        $teacher_ids = $request->get('teacher_ids');
        $student_ids = $request->get('student_ids');

        if (!empty($teacher_ids)) {
            $data['teachers'] = User::select('id', 'full_name')
                ->whereIn('id', $teacher_ids)->get();
        }

        if (!empty($student_ids)) {
            $data['students'] = User::select('id', 'full_name')
                ->whereIn('id', $student_ids)->get();
        }

        return view('admin.certificates.lists', $data);
    }

    private function filters($query, $request)
    {
        $filters = $request->all();

        if (!empty($filters['student_ids'])) {
            $query->whereIn('student_id', $filters['student_ids']);
        }

        if (!empty($filters['teacher_ids'])) {
            $quizzes = Quiz::whereIn('creator_id', $filters['teacher_ids'])->pluck('id')->toArray();

            if ($quizzes and is_array($quizzes)) {
                $query->whereIn('quiz_id', $quizzes);
            }
        }

        if (!empty($filters['quiz_title'])) {
            $quizzes = Quiz::whereTranslationLike('title', '%' . $filters['quiz_title'] . '%')->pluck('id')->toArray();
            $query->whereIn('quiz_id', $quizzes);
        }

        return $query;
    }

    public function CertificatesTemplatesList(Request $request)
    {
        $this->authorize('admin_certificate_template_list');

        removeContentLocale();

        $templates = CertificateTemplate::orderBy('created_at', 'desc')
            ->paginate(10);

        $data = [
            'pageTitle' => trans('admin/main.certificate_templates_list_page_title'),
            'templates' => $templates,
        ];

        return view('admin.certificates.templates', $data);
    }

    public function CertificatesNewTemplate()
    {
        $this->authorize('admin_certificate_template_create');

        removeContentLocale();

        $data = [
            'pageTitle' => trans('admin/main.certificate_new_template_page_title'),
        ];

        return view('admin.certificates.new_templates', $data);
    }

    public function CertificatesTemplateStore(Request $request, $template_id = null)
    {
        $this->authorize('admin_certificate_template_create');

        $rules = [
            'title' => 'required',
            'image' => 'required',
            'body' => 'required',
            'type' => 'required|in:quiz,course',
            'position_x' => 'required',
            'position_y' => 'required',
            'font_size' => 'required',
            'text_color' => 'required',
        ];
        $this->validate($request, $rules);

        $data = $request->all();

        if ($data['status'] and $data['status'] == 'publish') { // set draft for other templates
            CertificateTemplate::where('status', 'publish')
                ->where('type', $data['type'])
                ->update([
                    'status' => 'draft'
                ]);
        }

        if (!empty($template_id)) {

            $template = CertificateTemplate::findOrFail($template_id);
            $template->update([
                'image' => $data['image'],
                'status' => $data['status'],
                'type' => $data['type'],
                'position_x' => $data['position_x'],
                'position_y' => $data['position_y'],
                'font_size' => $data['font_size'],
                'text_color' => $data['text_color'],
                'updated_at' => time(),
            ]);
        } else {
            $template = CertificateTemplate::create([
                'image' => $data['image'],
                'status' => $data['status'],
                'type' => $data['type'],
                'position_x' => $data['position_x'],
                'position_y' => $data['position_y'],
                'font_size' => $data['font_size'],
                'text_color' => $data['text_color'],
                'created_at' => time(),
            ]);
        }

        CertificateTemplateTranslation::updateOrCreate([
            'certificate_template_id' => $template->id,
            'locale' => mb_strtolower($data['locale']),
        ], [
            'title' => $data['title'],
            'body' => $data['body'],
            'rtl' => $data['rtl'],
        ]);

        removeContentLocale();

        return redirect('/admin/certificates/templates');
    }

    public function CertificatesTemplatePreview(Request $request)
    {
        $this->authorize('admin_certificate_template_create');

        $data = [
            'pageTitle' => trans('public.certificate'),
            'image' => $request->get('image'),
            'body' => $request->get('body'),
            'position_x' => (int)$request->get('position_x', 120),
            'position_y' => (int)$request->get('position_y', 100),
            'font_size' => (int)$request->get('font_size', 26),
            'text_color' => $request->get('text_color', '#e1e1e1'),
        ];

        $isRtl = $request->get('rtl', false);

        $body = str_replace('[student]', 'student name', $data['body']);
        $body = str_replace('[course]', 'course name', $body);
        $body = str_replace('[grade]', 'xx', $body);
        $body = str_replace('[certificate_id]', 'xx', $body);
        $body = str_replace('[user_certificate_additional]', 'xx', $body);
        $body = str_replace('[date]', 'xx', $body);
        $body = str_replace('[instructor_name]', 'xx', $body);
        $body = str_replace('[duration]', 'xx', $body);
        $body = str_replace('[user_serial_number]', 'USR-2024-000001', $body);

//        if ($isRtl) {
        $Arabic = new \I18N_Arabic('Glyphs');
        $body = $Arabic->utf8Glyphs($body);
//        }

        $imgPath = public_path($data['image']);
        $img = Image::make($imgPath);

        $img->text($body, $data['position_x'], $data['position_y'], function ($font) use ($data, $isRtl) {
            $font->file(public_path('assets/default/fonts/IBMPlexSansArabic-Medium.ttf'));
            $font->size($data['font_size']);
            $font->color($data['text_color']);
            $font->align('center');
        });
        return $img->response('png');
    }

    public function CertificatesTemplatesEdit(Request $request, $template_id)
    {
        $this->authorize('admin_certificate_template_edit');

        $template = CertificateTemplate::findOrFail($template_id);

        $locale = $request->get('locale', app()->getLocale());
        storeContentLocale($locale, $template->getTable(), $template->id);

        $data = [
            'pageTitle' => trans('admin/main.certificate_template_edit_page_title'),
            'template' => $template
        ];
        return view('admin.certificates.new_templates', $data);
    }

    public function CertificatesTemplatesDelete($template_id)
    {
        $this->authorize('admin_certificate_template_delete');

        $template = CertificateTemplate::findOrFail($template_id);

        $template->delete();

        return redirect('/admin/certificates/templates');
    }

    public function CertificatesDownload($id)
    {
        $certificate = Certificate::findOrFail($id);


        $makeCertificate = new MakeCertificate();

        if ($certificate->type == 'quiz') {
            $quizResult = QuizzesResult::where('id', $certificate->quiz_result_id)
                ->where('status', QuizzesResult::$passed)
                ->with([
                    'quiz' => function ($query) {
                        $query->with(['webinar']);
                    },
                    'user'
                ])
                ->first();

            return $makeCertificate->makeQuizCertificate($quizResult);
        } else if ($certificate->type == 'course') {

            return $makeCertificate->makeCourseCertificate($certificate);
        }

        else{
            return $makeCertificate->makeImtmontessoriCertificate($certificate);
        }

        abort(404);
    }

    public function exportExcel(Request $request)
    {
        $this->authorize('admin_certificate_export_excel');

        $query = Certificate::query();

        $query = $this->filters($query, $request);

        $certificates = $query->with(
            [
                'quiz' => function ($query) {
                    $query->with('webinar');
                },
                'student',
                'quizzesResult'
            ]
        )->orderBy('created_at', 'desc')
            ->get();

        $export = new CertificatesExport($certificates);

        return Excel::download($export, 'certificates.xlsx');
    }

    public function imtmontessori_certificates(Request $request)
    {


        $this->authorize('admin_certificate_list');

        $query = Certificate::where('type','imtmontessori');

        $query = $this->imtmontessori_filters($query, $request);

        $certificates = $query->with(
            [

                'student',

            ]
        )->orderBy('created_at', 'desc')
            ->paginate(10);


        $data = [
            'pageTitle' => trans('admin/main.certificate_list_page_title'),
            'certificates' => $certificates,
            'student' => $filters['student'] ?? null,
            //'instructor' => $filters['instructor'] ?? null,
            //'quiz_title' => $filters['quiz_title'] ?? null,
        ];

        //$teacher_ids = $request->get('teacher_ids');
        $student_ids = $request->get('student_ids');

        /*if (!empty($teacher_ids)) {
            $data['teachers'] = User::select('id', 'full_name')
                ->whereIn('id', $teacher_ids)->get();
        }*/

        if (!empty($student_ids)) {
            $data['students'] = User::select('id', 'full_name')
                ->whereIn('id', $student_ids)->get();
        }

        return view('admin.certificates.imtmontessori', $data);
    }

    private function imtmontessori_filters($query, $request)
    {
        $filters = $request->all();

        if (!empty($filters['student_ids'])) {
            $query->whereIn('student_id', $filters['student_ids']);
        }



        return $query;
    }

    public function create_imtmontessori(){
        $templates = CertificateTemplateIMT::all();

        $data = [
            'pageTitle' => 'إنشاء شهادة',
            'templates' => $templates,
        ];

        return view('admin.certificates.new_imtmontessori', $data);
    }

    public function store_imtmontessori(Request $request){
        $rules = [
            'template'=>'required',
//            'age' => 'required_if:type,normal',
            'age' => 'nullable',
            'date' => 'required',
            'student_id' => 'required|exists:users,id',
            'dimensions_type' => 'required',

        ];
        $this->validate($request, $rules);
        //$data = $request->all();

        if ($request->age && $request->age != null){
            $type = 'normal';
        }else{
            $type = 'special';
        }

        Certificate::create([
            'student_id'=>$request->student_id,
            'age'   =>$request->age,
            'date'   =>$request->date,
            'type'  =>'imtmontessori',
            'student_type'=>$type,
            'created_at' => time(),
            'template'=>$request->template,
            'dimensions_type'=>$request->dimensions_type,
        ]);

        return redirect('/admin/certificates/imtmontessori-certificates')->with(['success'=>'تم الإنشاء بنجاح']);
    }

    public function CertificatesImtmontessoriPreview(Request $request)
    {
        $request->validate([
            'template' => 'required',
            'date' => 'required',
        ]);

        $Arabic = new \I18N_Arabic('Glyphs');
        $student=User::find($request->student_id)->full_name;
        $name = $Arabic->utf8Glyphs($student);
        $age = $request->age;
        $date = $request->date;
        // Get user's serial number for display
        $user = User::find($request->student_id);
        $s_no = $user ? $user->getSerialNumber() : 'N/A';

        // Check if template file exists
        $templatePath = public_path($request->template);
        if (!file_exists($templatePath)) {
            abort(404, 'Certificate template image not found: ' . $request->template);
        }
        $img=Image::make($templatePath);

//        if($request->age){
//
//            $img=Image::make(public_path('certificate-3-6.jpeg'));
//        } else {
//
//            $img=Image::make(public_path('special_certificate.jpeg'));
//
//        }

//        $uploadedImage = $request->file('image');
//        $img = Image::make($uploadedImage);
//        $img->save(public_path('preview_certificate.jpeg'));




        // Add other text elements
//        $img->text($age, 810, 1050, function ($font) use ($fontPath) {
//            $font->file($fontPath);
//            $font->size(33);
//            $font->color("#ff3132");
//        });


        if ($request->dimensions_type === 'height'){
            // Fixed center position for name - always in same place
            $fontSize = 45; // Reduced font size
            $fontPath = public_path('assets/default/fonts/IBMPlexSansArabic-Medium.ttf');
            $imageWidth = $img->width();
            // Fixed center position - always in same place
            $xPosition = $imageWidth / 2;

            // Add centered text
            $img->text($name, $xPosition, 680, function ($font) use ($fontPath, $fontSize) {
                $font->file($fontPath);
                $font->size($fontSize);
                $font->color("#cf9e5e");
                $font->align('center');
            });
            $img->text($date, 200, 1380, function ($font) use ($fontPath) {
                $font->file($fontPath);
                $font->size(37);
                $font->color("#000");
            });
            $img->text(11111111, 700, 1380, function ($font) use ($fontPath) {
                $font->file($fontPath);
                $font->size(30);
                $font->color("#000");
            });
        }elseif ($request->dimensions_type === 'width1'){
            // Fixed center position for name - always in same place
            $fontSize = 85; // Reduced font size
            $fontPath = public_path('assets/default/fonts/GreatVibes-Regular.ttf');

            // Get the image width
            $imageWidth = $img->width();

            // Fixed center position - always in same place
            $xPosition = $imageWidth / 2;

            // Add centered text
            $img->text(ucfirst(strtolower($name)), $xPosition, 430, function ($font) use ($fontPath, $fontSize) {
                $font->file($fontPath);
                $font->size($fontSize);
                $font->color("#bb944c");
                $font->align('center');
            });
            $img->text($date, 1200, 870, function ($font) use ($fontPath) {
                $font->file($fontPath);
                $font->size(40);
                $font->color("#000");
            });
            $img->text(11111111, 420, 890, function ($font) use ($fontPath) {
                $font->file($fontPath);
                $font->size(40);
                $font->color("#000");
            });
        }elseif($request->dimensions_type === 'width2'){
            // Fixed center position for name - always in same place
            $fontSize = 85; // Reduced font size
            $fontPath = public_path('assets/default/fonts/GreatVibes-Regular.ttf');

            // Get the image width
            $imageWidth = $img->width();

            // Fixed center position - always in same place
            $xPosition = $imageWidth / 2;

            // Add centered text
            $img->text(ucfirst(strtolower($name)), $xPosition, 520, function ($font) use ($fontPath, $fontSize) {
                $font->file($fontPath);
                $font->size($fontSize);
                $font->color("#bb944c");
                $font->align('center');
            });
            $img->text($date, 450, 980, function ($font) use ($fontPath) {
                $font->file($fontPath);
                $font->size(40);
                $font->color("#000");
            });
            $img->text(11111111, 1070, 980, function ($font) use ($fontPath) {
                $font->file($fontPath);
                $font->size(40);
                $font->color("#000");
            });
        }



        return $img->response('png');

        /*$file_name=$certificate->slug.'.jpeg';
        $img->save(storage_path('app/public/'.$file_name));
        return response()->download(storage_path('app/public/'.$file_name));*/



        /*$data = [
            'pageTitle' => trans('public.certificate'),
            'image' => $request->get('image'),
            'body' => $request->get('body'),
            'position_x' => (int)$request->get('position_x', 120),
            'position_y' => (int)$request->get('position_y', 100),
            'font_size' => (int)$request->get('font_size', 26),
            'text_color' => $request->get('text_color', '#e1e1e1'),
        ];

        $isRtl = $request->get('rtl', false);

        $body = str_replace('[student]', 'student name', $data['body']);
        $body = str_replace('[course]', 'course name', $body);
        $body = str_replace('[grade]', 'xx', $body);
        $body = str_replace('[certificate_id]', 'xx', $body);
        $body = str_replace('[user_certificate_additional]', 'xx', $body);
        $body = str_replace('[date]', 'xx', $body);
        $body = str_replace('[instructor_name]', 'xx', $body);
        $body = str_replace('[duration]', 'xx', $body);*/

        //$data['body'] = $body;//mb_convert_encoding($body, 'HTML-ENTITIES', 'UTF-8');;

//        if ($isRtl) {
        //$Arabic = new \I18N_Arabic('Glyphs');
        //$body = $Arabic->utf8Glyphs($body);
//        }

        /* $imgPath = public_path($data['image']);
         $img = Image::make($imgPath);

         $img->text($body, $data['position_x'], $data['position_y'], function ($font) use ($data, $isRtl) {
             $font->file(public_path('assets/default/fonts/IBMPlexSansArabic-Medium.ttf'));
             $font->size($data['font_size']);
             $font->color($data['text_color']);
         });
         return $img->response('png');*/
    }


    public function edit(Request $request, $id){
        $certificate = Certificate::findOrFail($id);



        $data = [
            'pageTitle' => 'تعديل شهادة',
            'certificate' => $certificate,
        ];

        return view('admin.certificates.edit', $data);
    }

    public function update(Request $request, $id){
        $rules = [
            'template'=>'required',
//            'age' => 'required_if:type,normal',
            'age' => 'nullable',
            'date' => 'required',
            'student_id' => 'required|exists:users,id',

        ];
        $this->validate($request, $rules);

        //$data = $request->all();
        /*do {
                   $s_no = rand(100000,1000000);
                } while (Certificate::where('serial_number',$s_no)->count());*/

        if ($request->age && $request->age != null){
            $type = 'normal';
        }else{
            $type = 'special';
        }

        $certificate = Certificate::findOrFail($id);
        $certificate->update([
            'student_id'=>$request->student_id,
            'age'   =>$request->age,
            'date'   =>$request->date,
            //'type'  =>'imtmontessori',
            'student_type'=>$type,
            'updated_at' => time(),
            'template'=>$request->template,

            //'serial_number'=>$s_no
        ]);

        return redirect('/admin/certificates/imtmontessori-certificates')->with(['success'=>'تم التعديل بنجاح']);
    }

    public function destroy($id){
        $certificate = Certificate::findOrFail($id);
        $certificate->delete();
        return redirect('/admin/certificates/imtmontessori-certificates')->with(['success'=>'تم التعديل بنجاح']);
    }
}
