<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class UpdateUniversityNotificationDisplayLabels extends Migration
{
    public function up()
    {
        if (!Schema::hasTable('sm_notification_settings')) {
            return;
        }

        DB::table('sm_notification_settings')
            ->select('id', 'subject', 'template')
            ->orderBy('id')
            ->chunkById(100, function ($settings) {
                foreach ($settings as $setting) {
                    $updates = [];

                    foreach (['subject', 'template'] as $column) {
                        $updated = $this->replaceColumnValue($setting->{$column});

                        if ($updated !== $setting->{$column}) {
                            $updates[$column] = $updated;
                        }
                    }

                    if (!empty($updates)) {
                        DB::table('sm_notification_settings')
                            ->where('id', $setting->id)
                            ->update($updates);
                    }
                }
            });
    }

    public function down()
    {
        // Display-only university labels are intentionally not reverted.
    }

    private function replaceColumnValue($value)
    {
        if ($value === null || $value === '') {
            return $value;
        }

        $decoded = json_decode($value, true);

        if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) {
            $updated = $this->replaceVisibleStrings($decoded);

            return json_encode($updated);
        }

        if (is_string($value)) {
            return $this->replaceVisibleTerms($value);
        }

        return $value;
    }

    private function replaceVisibleStrings($value)
    {
        if (is_array($value)) {
            foreach ($value as $key => $item) {
                $value[$key] = $this->replaceVisibleStrings($item);
            }

            return $value;
        }

        if (is_string($value)) {
            return $this->replaceVisibleTerms($value);
        }

        return $value;
    }

    private function replaceVisibleTerms($value)
    {
        $replacements = [
            'Assigned Classes For Teachers' => 'Academic Advisor Assigned',
            'Class: [class], Section: [section], Subject: [subject]' => 'Programme: [class], Academic Group: [section], Unit: [subject]',
            'Class: [class], Section: [section]' => 'Programme: [class], Academic Group: [section]',
            'A Assignment:' => 'An Assignment:',
            'New assigned subject' => 'New assigned unit',
            'A subject [subject]' => 'A unit [subject]',
            'Subject Wise Attendance' => 'Unit Wise Attendance',
            'Subject: [subject]' => 'Unit: [subject]',
            'Subject [subject]' => 'Unit [subject]',
            'Assign Dormitory' => 'Assign Hostel',
            'A Dormitory: [dormitory]' => 'A Hostel: [dormitory]',
            'Homeworks' => 'Assignments',
            'Homework' => 'Assignment',
            'Subjects' => 'Units',
            'Subject' => 'Unit',
            'Teachers' => 'Lecturers',
            'Teacher' => 'Lecturer',
            'Dormitories' => 'Hostels',
            'Dormitory' => 'Hostel',
            'Sections' => 'Academic Groups',
            'Section' => 'Academic Group',
        ];

        return str_replace(array_keys($replacements), array_values($replacements), $value);
    }
}
