<?php

use Phalcon\Mvc\Model\Migration;

class KeluhanBPJSKesehatanMigration_515 extends Migration {
    public function up() {
        $this->down();

        self::$connection->execute("
            INSERT INTO jaga.roles(name, label, description) VALUES ('admin_keluhan_bpjs_klop', 'Administrator KLOP Keluhan BPJS Kesehatan', 'Administrator KLOP Keluhan BPJS Kesehatan');
        ");

        self::$connection->execute("
            INSERT INTO jaga.scopes(name, label, description) VALUES ('keluhan.bpjs.follow_up', 'Melakukan followup keluhan BPJS Kesehatan', 'Melakukan followup keluhan BPJS Kesehatan');
        ");

        self::$connection->execute("
            INSERT INTO jaga.roles_scopes(role_id, scope_id) values
				((select id from jaga.roles where name = 'admin_keluhan_bpjs_klop'), (select id from jaga.scopes where name = 'discussion.create')), 
				((select id from jaga.roles where name = 'admin_keluhan_bpjs_klop'), (select id from jaga.scopes where name = 'discussion.react')), 
				((select id from jaga.roles where name = 'admin_keluhan_bpjs_klop'), (select id from jaga.scopes where name = 'report.create')), 
				((select id from jaga.roles where name = 'admin_keluhan_bpjs_klop'), (select id from jaga.scopes where name = 'keluhan.bpjs.follow_up')) 
            ;
        ");

        self::$connection->execute("
            SELECT setval('jaga.topik_diskusi_bansos_id_seq', COALESCE((SELECT MAX(id)+1 FROM jaga.topik_diskusi_bansos), 1), false);
            
            INSERT INTO jaga.topik_diskusi_bansos (deskripsi, deskripsi_en, type, view_order) VALUES ('BPJS Kesehatan - Keluhan Layanan', 'Social Security Administrator for Health - Service Complaint', 'BPJS', 1);
            INSERT INTO jaga.topik_diskusi_bansos (deskripsi, deskripsi_en, type, view_order) VALUES ('BPJS Kesehatan - Keluhan Obat', 'Social Security Administrator for Health - Drug Complaint', 'BPJS', 2);
            INSERT INTO jaga.topik_diskusi_bansos (deskripsi, deskripsi_en, type, view_order) VALUES ('BPJS Kesehatan - Keluhan Tindakan', 'Social Security Administrator for Health - Treatment Complaint', 'BPJS', 3);
            INSERT INTO jaga.topik_diskusi_bansos (deskripsi, deskripsi_en, type, view_order) VALUES ('BPJS Kesehatan - Lainnya', 'Social Security Administrator for Health - Other', 'BPJS', 4);
        ");

        self::$connection->execute("
            ALTER TABLE jaga.diskusi_objek
                ADD COLUMN IF NOT EXISTS objek_type character varying(255);
        ");
    }

    public function down() {
        self::$connection->execute("
            ALTER TABLE jaga.diskusi_objek
                DROP COLUMN IF EXISTS objek_type;
        ");

        self::$connection->execute("
            DELETE FROM jaga.topik_diskusi_bansos WHERE type = 'BPJS';
            SELECT setval('jaga.topik_diskusi_bansos_id_seq', COALESCE((SELECT MAX(id)+1 FROM jaga.topik_diskusi_bansos), 1), false);
        ");

        self::$connection->execute("
            DELETE FROM jaga.roles_scopes WHERE 
            role_id = (select id from jaga.roles where name = 'admin_keluhan_bpjs_klop');
            SELECT setval(pg_get_serial_sequence('roles_scopes', 'id'), coalesce(max(id),0) + 1, false) FROM jaga.roles_scopes;
        ");

        self::$connection->execute("    
            DELETE FROM jaga.scopes WHERE name = 'keluhan.bpjs.follow_up';
            SELECT setval(pg_get_serial_sequence('scopes', 'id'), coalesce(max(id),0) + 1, false) FROM jaga.scopes;
        ");

        self::$connection->execute("
            DELETE FROM jaga.roles WHERE name = 'admin_keluhan_bpjs_klop';
            SELECT setval(pg_get_serial_sequence('roles', 'id'), coalesce(max(id),0) + 1, false) FROM jaga.roles;
        ");
    }
}