-- Migration 004: central-port dispatch + NPC factions + unload lifecycle
-- Run once against an existing skyrama_clone database.
-- Row seeding (NPC users, hub countries, central airports, factions) is done
-- idempotently in Go by Client.EnsureStage0World() at startup, not here.

-- Central ports are system-owned (faction NPC) airports, one per continent.
ALTER TABLE airports
    ADD COLUMN is_central BOOLEAN NOT NULL DEFAULT FALSE;

-- NPC faction-leader users: cannot log in (empty password_hash), hidden from
-- login + user lists.
ALTER TABLE users
    ADD COLUMN is_npc BOOLEAN NOT NULL DEFAULT FALSE;

-- Where a dispatched plane is flying to, plus a post-landing 'unloading' phase.
ALTER TABLE player_planes
    MODIFY COLUMN status ENUM('idle','maintenance','boarding','taxiing','flying','unloading') NOT NULL DEFAULT 'idle',
    ADD COLUMN destination_airport_id INT UNSIGNED DEFAULT NULL,
    ADD FOREIGN KEY (destination_airport_id) REFERENCES airports(id);

-- One NPC faction per continent; owns that continent's central port.
CREATE TABLE IF NOT EXISTS factions (
    id integer unsigned primary key auto_increment,
    name varchar(255) not null,
    continent ENUM('void','Africa','Antarctica','Asia','Europe','North America','Oceania','South America') NOT NULL UNIQUE,
    leader_name varchar(255) not null,
    background TEXT,
    user_id integer not null,
    central_airport_id integer unsigned not null,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    foreign key (user_id) references users(id),
    foreign key (central_airport_id) references airports(id)
);
