# Generated by Django 3.2.20 on 2023-08-02 07:24

from django.db import migrations


class Migration(migrations.Migration):
    dependencies = [
        ('nautobot_cable_utils', '0008_auto_20230324_1055'),
    ]

    operations = [
        migrations.RunSQL(
            sql="""
                create or replace function cast_to_int(text, integer) returns integer as $$
                begin
                    return cast($1 as integer);
                exception
                    when invalid_text_representation then
                        return $2;
                end;
                $$ language plpgsql immutable;
                CREATE OR REPLACE FUNCTION get_numberscheme (rack varchar) RETURNS varchar AS $$ BEGIN
                    return case
                        when rack like '1519-%' then 'grz'
                        when rack like 'V1519.%' then 'goenet'
                        when rack like 'V2356.%' then 'goenet'
                        else 'unknown'
                    end;
                END;
                $$ LANGUAGE plpgsql;
                CREATE OR REPLACE FUNCTION is_neighbor (r1 varchar, r2 varchar) RETURNS bool AS $$ DECLARE
                    numberscheme varchar;
                BEGIN
                    numberscheme = get_numberscheme(r1);
                    CASE
                        WHEN numberscheme != get_numberscheme(r2) THEN RETURN false;
                        WHEN numberscheme = 'unknown' THEN RETURN false;
                        WHEN numberscheme = 'goenet' THEN
                            CASE
                                WHEN substr(r1, 1, 8) != substr(r2, 1, 8) THEN RETURN false;
                                WHEN ABS(cast_to_int(substr(r1, 9, 1), 0) - cast_to_int(substr(r2, 9, 1), 0)) = 1 THEN RETURN true;
                                ELSE RETURN false;
                            END CASE;
                        WHEN numberscheme = 'grz' THEN
                            CASE
                                WHEN substr(r1, 1, 9) != substr(r2, 1, 9) THEN RETURN false;
                                WHEN ABS(cast_to_int(substr(r1, 10, 2), 0) - cast_to_int(substr(r2, 10, 2), 0)) = 1 THEN RETURN true;
                                WHEN ABS(cast_to_int(substr(r1, 10, 2), 0) - cast_to_int(substr(r2, 10, 2), 0)) = 2 THEN
                                    CASE
                                        WHEN (
                                            select count(*) from dcim_rack
                                            where name like (
                                                substr(r1, 1, 9) ||
                                                LPAD(CAST(GREATEST(
                                                    cast_to_int(substr(r1, 10, 2), 0)-1,
                                                    cast_to_int(substr(r2, 10, 2), 0)-1
                                                ) as varchar), 2, '0') ||
                                                '%')
                                         ) = 0 THEN RETURN true;
                                        ELSE RETURN false;
                                    END CASE;
                                ELSE RETURN false;
                            END CASE;
                        ELSE
                            RETURN false;
                    END CASE;
                END;
                $$ LANGUAGE plpgsql;
            """
        )
    ]
