<?php

use yii\db\Migration;

/**
 * Creates leads table for lead management module.
 */
class m240001_000001_create_leads_table extends Migration
{
    public function safeUp(): void
    {
        $this->createTable('{{%leads}}', [
            'id'         => $this->primaryKey()->unsigned()->comment('Primary key'),
            'name'       => $this->string(100)->notNull()->comment('Lead name'),
            'phone'      => $this->string(20)->notNull()->comment('Phone number'),
            'email'      => $this->string(150)->null()->comment('Email address'),
            'source'     => $this->string(50)->null()->comment('Lead source: website, referral, social, walk-in'),
            'interest'   => $this->string(100)->null()->comment('Product/plan interest'),
            'message'    => $this->text()->null()->comment('Message from lead'),
            'status'     => "ENUM('new','contacted','converted','lost') NOT NULL DEFAULT 'new' COMMENT 'Lead status'",
            'created_at' => $this->integer()->unsigned()->notNull()->comment('Unix timestamp of creation'),
            'updated_at' => $this->integer()->unsigned()->notNull()->comment('Unix timestamp of last update'),
        ], 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT="Lead management"');

        $this->createIndex('idx_leads_phone', '{{%leads}}', 'phone');
        $this->createIndex('idx_leads_email', '{{%leads}}', 'email');
        $this->createIndex('idx_leads_status', '{{%leads}}', 'status');
        $this->createIndex('idx_leads_created_at', '{{%leads}}', 'created_at');
    }

    public function safeDown(): void
    {
        $this->dropTable('{{%leads}}');
    }
}
