<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Shared staff-review inbox for every guest-facing AI-drafted message —
 * post-checkout thank-you first, pre-arrival upsell next, sharing this
 * one table/model/controller rather than one pair per category, since
 * "draft queued, staff reviews, staff sends or dismisses, unless the
 * tenant opted this category into autosend" is exactly the same shape
 * for both (see the Phase 5 AI brief's guest-facing guardrails).
 *
 * `status`: pending (awaiting review), sent (staff clicked Send, or
 * autosend fired), dismissed (staff declined to send), failed (send was
 * attempted — manually or via autosend — and the SMS gateway failed).
 * `sent_by` is null for an autosend; a real user id for a manual send.
 * A real send (manual or auto) also writes to message_log (channel
 * 'sms') via MessageLogModel::record(), same as every other outbound
 * message in this app — this table is the review queue, message_log
 * stays the single source of truth for "was this actually sent".
 */
class CreateAiMessageDraftsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'category' => [
                'type'       => 'VARCHAR',
                'constraint' => 40,
                'comment'    => 'post_checkout_thankyou, pre_arrival_upsell',
            ],
            'guest_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'booking_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'to_phone' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
            ],
            'draft_text' => [
                'type' => 'TEXT',
            ],
            'status' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'default'    => 'pending',
                'comment'    => 'pending, sent, dismissed, failed',
            ],
            'data_snapshot' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'sent_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'sent_by' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey('status');
        $this->forge->addKey('category');
        $this->forge->addForeignKey('guest_id', 'guests', 'id', '', 'SET NULL');
        $this->forge->addForeignKey('booking_id', 'bookings', 'id', '', 'SET NULL');
        $this->forge->addForeignKey('sent_by', 'users', 'id', '', 'SET NULL');
        $this->forge->createTable('ai_message_drafts');
    }

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