<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateTibaLetaCore extends Migration
{
    public function up()
    {
        $sql = file_get_contents(ROOTPATH . 'database/tibaleta.sql');
        if ($sql === false) throw new \RuntimeException('Unable to read TibaLeta schema.');
        $sql = preg_replace('/^\s*--.*$/m', '', $sql);
        foreach (preg_split('/;\s*(?:\r?\n|$)/', (string)$sql) as $statement) {
            $statement = trim($statement);
            if ($statement !== '') $this->db->query($statement);
        }
    }

    public function down()
    {
        $tables = ['audit_logs','consents','promo_codes','subscriptions','care_packages','telehealth_sessions','notifications','messages','reviews','payouts','payments','partner_referrals','pharmacy_orders','lab_results','lab_requests','partners','prescription_items','prescriptions','medications','allergies','patient_conditions','clinical_notes','visit_vitals','visits','appointments','care_request_assignments','care_requests','practitioner_locations','practitioner_availability','practitioner_services','practitioner_specialties','practitioner_credentials','practitioner_profiles','services','specialties','addresses','family_members','patient_profiles','password_reset_tokens','api_tokens','users'];
        $this->db->disableForeignKeyChecks();
        foreach ($tables as $table) $this->forge->dropTable($table, true);
        $this->db->enableForeignKeyChecks();
    }
}
