<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Executive Dining membership — separate from the loyalty programme
 * (Phase 3's other half). Loyalty is transactional (points earned on
 * every payment, any guest can have an account); this is an opt-in,
 * staff-enrolled club with a small number of members and a renewal
 * date, closer in shape to `corporate_accounts` than to
 * `loyalty_accounts`. Benefit text ("2 free desserts/month") stays
 * manager-editable free text in Settings per tier rather than a rules
 * engine — matches how `hotel_services`/`corporate_accounts` stay
 * simple records instead of over-engineered systems.
 *
 * `benefit_usage_log` makes "benefits used this cycle" a real, queryable
 * fact instead of just a claim on a card.
 */
class CreateDiningMembershipTables 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,
            ],
            'tier' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'comment'    => 'silver|gold|platinum — see Config\\Lookups::$diningMembershipTiers.',
            ],
            'joined_at' => [
                'type' => 'DATE',
            ],
            'renewal_date' => [
                'type' => 'DATE',
                'null' => true,
            ],
            'status' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'default'    => 'active',
                'comment'    => 'active|expired|cancelled',
            ],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('guest_id');
        $this->forge->addForeignKey('guest_id', 'guests', 'id', '', 'CASCADE');
        $this->forge->createTable('dining_memberships');

        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'membership_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'benefit_description' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
            ],
            'used_at' => [
                'type' => 'DATETIME',
            ],
            'pos_order_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey('membership_id');
        $this->forge->addForeignKey('membership_id', 'dining_memberships', 'id', '', 'CASCADE');
        $this->forge->addForeignKey('pos_order_id', 'pos_orders', 'id', '', 'SET NULL');
        $this->forge->createTable('benefit_usage_log');
    }

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