<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Every outbound WhatsApp/SMS send is logged here regardless of which
 * channel(s) actually got used — a hotel owner will ask "did the guest
 * actually get the confirmation?" (Phase 3 brief), and this is the answer.
 */
class CreateMessageLogTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'guest_id' => [
                'type'     => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'null'     => true,
            ],
            'channel' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'comment'    => 'whatsapp, sms',
            ],
            'message_type' => [
                'type'       => 'VARCHAR',
                'constraint' => 40,
                'comment'    => 'booking_confirmation, pre_arrival_reminder, post_stay_feedback, deposit_request',
            ],
            'to_phone' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
            ],
            'status' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'comment'    => 'sent, failed',
            ],
            'provider_response' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey('guest_id');
        $this->forge->addKey('message_type');
        $this->forge->addForeignKey('guest_id', 'guests', 'id', '', 'SET NULL');
        $this->forge->createTable('message_log');
    }

    public function down()
    {
        $this->forge->dropTable('message_log', true);
    }
}
