<?php

use App\Models\SmNotificationSetting;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class AddNotificationLogsAndAdmitCardAcademicSettings extends Migration
{
    public function up()
    {
        if (!Schema::hasTable('notification_logs')) {
            Schema::create('notification_logs', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->unsignedInteger('user_id')->nullable()->index();
                $table->string('type', 80)->index();
                $table->string('status', 20)->index();
                $table->text('error_message')->nullable();
                $table->timestamp('created_at')->nullable()->useCurrent();
            });
        }

        $this->extendAdmitCardSettings();
        $this->seedNotificationEvents();
    }

    public function down()
    {
        if (Schema::hasTable('admit_card_settings')) {
            Schema::table('admit_card_settings', function (Blueprint $table) {
                foreach (['show_subject_list', 'show_rules_section', 'enable_signature_columns'] as $column) {
                    if (Schema::hasColumn('admit_card_settings', $column)) {
                        $table->dropColumn($column);
                    }
                }
            });
        }

        if (Schema::hasTable('sm_notification_settings')) {
            DB::table('sm_notification_settings')
                ->whereIn('event', [
                    'Homework_Updated',
                    'Homework_Evaluation',
                    'Evalution_homework',
                    'Results_Published',
                ])
                ->delete();
        }

        Schema::dropIfExists('notification_logs');
    }

    private function extendAdmitCardSettings()
    {
        if (!Schema::hasTable('admit_card_settings')) {
            return;
        }

        Schema::table('admit_card_settings', function (Blueprint $table) {
            if (!Schema::hasColumn('admit_card_settings', 'show_subject_list')) {
                $table->boolean('show_subject_list')->default(1)->after('parent_notification');
            }
            if (!Schema::hasColumn('admit_card_settings', 'show_rules_section')) {
                $table->boolean('show_rules_section')->default(1)->after('show_subject_list');
            }
            if (!Schema::hasColumn('admit_card_settings', 'enable_signature_columns')) {
                $table->boolean('enable_signature_columns')->default(1)->after('show_rules_section');
            }
        });
    }

    private function seedNotificationEvents()
    {
        if (!Schema::hasTable('sm_notification_settings')) {
            return;
        }

        $schoolIds = [1];
        if (Schema::hasTable('sm_schools')) {
            $ids = DB::table('sm_schools')->pluck('id')->toArray();
            $schoolIds = count($ids) ? $ids : $schoolIds;
        }

        $events = $this->notificationEvents();

        foreach ($schoolIds as $schoolId) {
            foreach ($events as $event) {
                $exists = SmNotificationSetting::where('event', $event['event'])
                    ->where('school_id', $schoolId)
                    ->exists();

                if ($exists) {
                    continue;
                }

                $setting = new SmNotificationSetting();
                $setting->event = $event['event'];
                $setting->destination = $event['destination'];
                $setting->recipient = $event['recipient'];
                $setting->subject = $event['subject'];
                $setting->template = $event['template'];
                $setting->shortcode = $event['shortcode'];
                $setting->school_id = $schoolId;
                $setting->save();
            }
        }
    }

    private function notificationEvents()
    {
        $destinations = [
            'Email' => 1,
            'SMS' => 1,
            'Web' => 1,
            'App' => 1,
        ];

        $recipients = [
            'Student' => 1,
            'Parent' => 1,
        ];

        return [
            [
                'event' => 'Homework_Updated',
                'destination' => $destinations,
                'recipient' => $recipients,
                'subject' => [
                    'Student' => 'Assignment Updated.',
                    'Parent' => 'Assignment Updated.',
                ],
                'template' => [
                    'Student' => [
                        'Email' => 'Dear [student_name], Your assignment for Unit: [subject] has been updated. For any query, please contact with admin.',
                        'SMS' => 'Dear [student_name], Your assignment for Unit: [subject] has been updated. For any query, please contact with admin.',
                        'Web' => 'Dear [student_name], Your assignment for Unit: [subject] has been updated. For any query, please contact with admin.',
                        'App' => 'Dear [student_name], Your assignment for Unit: [subject] has been updated. For any query, please contact with admin.',
                    ],
                    'Parent' => [
                        'Email' => 'Dear [parent_name], Your child [student_name] has an updated assignment for Unit: [subject]. For any query, please contact with admin.',
                        'SMS' => 'Dear [parent_name], Your child [student_name] has an updated assignment for Unit: [subject]. For any query, please contact with admin.',
                        'Web' => 'Dear [parent_name], Your child [student_name] has an updated assignment for Unit: [subject]. For any query, please contact with admin.',
                        'App' => 'Dear [parent_name], Your child [student_name] has an updated assignment for Unit: [subject]. For any query, please contact with admin.',
                    ],
                ],
                'shortcode' => [
                    'Student' => '[student_name], [subject]',
                    'Parent' => '[parent_name], [student_name], [subject]',
                ],
            ],
            [
                'event' => 'Homework_Evaluation',
                'destination' => $destinations,
                'recipient' => $recipients,
                'subject' => [
                    'Student' => 'Assignment Graded.',
                    'Parent' => 'Assignment Graded.',
                ],
                'template' => [
                    'Student' => [
                        'Email' => 'Dear [student_name], Your assignment for Unit: [subject] has been graded. For any query, please contact with admin.',
                        'SMS' => 'Dear [student_name], Your assignment for Unit: [subject] has been graded. For any query, please contact with admin.',
                        'Web' => 'Dear [student_name], Your assignment for Unit: [subject] has been graded. For any query, please contact with admin.',
                        'App' => 'Dear [student_name], Your assignment for Unit: [subject] has been graded. For any query, please contact with admin.',
                    ],
                    'Parent' => [
                        'Email' => 'Dear [parent_name], Your child [student_name] has graded assignment for Unit: [subject]. For any query, please contact with admin.',
                        'SMS' => 'Dear [parent_name], Your child [student_name] has graded assignment for Unit: [subject]. For any query, please contact with admin.',
                        'Web' => 'Dear [parent_name], Your child [student_name] has graded assignment for Unit: [subject]. For any query, please contact with admin.',
                        'App' => 'Dear [parent_name], Your child [student_name] has graded assignment for Unit: [subject]. For any query, please contact with admin.',
                    ],
                ],
                'shortcode' => [
                    'Student' => '[student_name], [subject]',
                    'Parent' => '[parent_name], [student_name], [subject]',
                ],
            ],
            [
                'event' => 'Evalution_homework',
                'destination' => $destinations,
                'recipient' => $recipients,
                'subject' => [
                    'Student' => 'Assignment Graded.',
                    'Parent' => 'Assignment Graded.',
                ],
                'template' => [
                    'Student' => [
                        'Email' => 'Dear [student_name], Your assignment for Unit: [subject] has been graded. For any query, please contact with admin.',
                        'SMS' => 'Dear [student_name], Your assignment for Unit: [subject] has been graded. For any query, please contact with admin.',
                        'Web' => 'Dear [student_name], Your assignment for Unit: [subject] has been graded. For any query, please contact with admin.',
                        'App' => 'Dear [student_name], Your assignment for Unit: [subject] has been graded. For any query, please contact with admin.',
                    ],
                    'Parent' => [
                        'Email' => 'Dear [parent_name], Your child [student_name] has graded assignment for Unit: [subject]. For any query, please contact with admin.',
                        'SMS' => 'Dear [parent_name], Your child [student_name] has graded assignment for Unit: [subject]. For any query, please contact with admin.',
                        'Web' => 'Dear [parent_name], Your child [student_name] has graded assignment for Unit: [subject]. For any query, please contact with admin.',
                        'App' => 'Dear [parent_name], Your child [student_name] has graded assignment for Unit: [subject]. For any query, please contact with admin.',
                    ],
                ],
                'shortcode' => [
                    'Student' => '[student_name], [subject]',
                    'Parent' => '[parent_name], [student_name], [subject]',
                ],
            ],
            [
                'event' => 'Results_Published',
                'destination' => $destinations,
                'recipient' => $recipients,
                'subject' => [
                    'Student' => 'Results Published.',
                    'Parent' => 'Results Published.',
                ],
                'template' => [
                    'Student' => [
                        'Email' => 'Dear [student_name], Results for [exam_schedule] have been published on [date]. For any query, please contact with admin.',
                        'SMS' => 'Dear [student_name], Results for [exam_schedule] have been published on [date]. For any query, please contact with admin.',
                        'Web' => 'Dear [student_name], Results for [exam_schedule] have been published on [date]. For any query, please contact with admin.',
                        'App' => 'Dear [student_name], Results for [exam_schedule] have been published on [date]. For any query, please contact with admin.',
                    ],
                    'Parent' => [
                        'Email' => 'Dear [parent_name], Results for [student_name] in [exam_schedule] have been published on [date]. For any query, please contact with admin.',
                        'SMS' => 'Dear [parent_name], Results for [student_name] in [exam_schedule] have been published on [date]. For any query, please contact with admin.',
                        'Web' => 'Dear [parent_name], Results for [student_name] in [exam_schedule] have been published on [date]. For any query, please contact with admin.',
                        'App' => 'Dear [parent_name], Results for [student_name] in [exam_schedule] have been published on [date]. For any query, please contact with admin.',
                    ],
                ],
                'shortcode' => [
                    'Student' => '[student_name], [exam_schedule], [date]',
                    'Parent' => '[parent_name], [student_name], [exam_schedule], [date]',
                ],
            ],
        ];
    }
}
