"""PRD §13.2 schema v1 — full store.sqlite3 tables and indexes."""

from __future__ import annotations

from typing import List


def migrations_v1() -> List[str]:
    return [
        # members & wallet (no FK to catalog yet)
        """
        CREATE TABLE IF NOT EXISTS store_members (
            id TEXT PRIMARY KEY,
            name TEXT NOT NULL,
            phone TEXT,
            birthday TEXT,
            due_date TEXT,
            note TEXT NOT NULL DEFAULT '',
            tags_json TEXT NOT NULL DEFAULT '[]',
            fields_json TEXT NOT NULL DEFAULT '{}',
            status TEXT NOT NULL DEFAULT 'active',
            created_at TEXT NOT NULL DEFAULT (datetime('now','localtime')),
            updated_at TEXT NOT NULL DEFAULT (datetime('now','localtime'))
        );
        CREATE INDEX IF NOT EXISTS idx_store_members_birthday ON store_members(birthday);
        CREATE INDEX IF NOT EXISTS idx_store_members_due_date ON store_members(due_date);

        CREATE TABLE IF NOT EXISTS store_custom_field_defs (
            entity TEXT NOT NULL,
            field_key TEXT NOT NULL,
            label TEXT NOT NULL,
            field_type TEXT NOT NULL DEFAULT 'text',
            options_json TEXT NOT NULL DEFAULT '[]',
            sort_order INTEGER NOT NULL DEFAULT 0,
            created_at TEXT NOT NULL DEFAULT (datetime('now','localtime')),
            updated_at TEXT NOT NULL DEFAULT (datetime('now','localtime')),
            PRIMARY KEY (entity, field_key)
        );
        CREATE INDEX IF NOT EXISTS idx_store_custom_field_defs_entity ON store_custom_field_defs(entity);

        CREATE TABLE IF NOT EXISTS store_wallet (
            member_id TEXT PRIMARY KEY REFERENCES store_members(id),
            balance REAL NOT NULL DEFAULT 0,
            total_recharge REAL NOT NULL DEFAULT 0,
            total_consume REAL NOT NULL DEFAULT 0,
            updated_at TEXT NOT NULL DEFAULT (datetime('now','localtime'))
        );

        CREATE TABLE IF NOT EXISTS store_followups (
            id TEXT PRIMARY KEY,
            member_id TEXT NOT NULL REFERENCES store_members(id),
            content TEXT NOT NULL DEFAULT '',
            contact_at TEXT,
            next_followup_date TEXT,
            created_at TEXT NOT NULL DEFAULT (datetime('now','localtime'))
        );
        CREATE INDEX IF NOT EXISTS idx_store_followups_next ON store_followups(next_followup_date);

        CREATE TABLE IF NOT EXISTS store_transactions (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            member_id TEXT NOT NULL REFERENCES store_members(id),
            type TEXT NOT NULL,
            amount REAL NOT NULL DEFAULT 0,
            ref_appt_id TEXT,
            ref_order_id TEXT,
            technician_id TEXT,
            meta_json TEXT NOT NULL DEFAULT '{}',
            created_at TEXT NOT NULL DEFAULT (datetime('now','localtime'))
        );

        CREATE TABLE IF NOT EXISTS store_consumption_log (
            member_id TEXT PRIMARY KEY REFERENCES store_members(id),
            last_visit_at TEXT,
            visit_count INTEGER NOT NULL DEFAULT 0,
            fav_service TEXT,
            updated_at TEXT NOT NULL DEFAULT (datetime('now','localtime'))
        );
        """,
        # catalog
        """
        CREATE TABLE IF NOT EXISTS catalog_services (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name TEXT NOT NULL UNIQUE,
            price REAL NOT NULL,
            duration_min INTEGER NOT NULL DEFAULT 60,
            status TEXT NOT NULL DEFAULT 'active',
            created_at TEXT NOT NULL DEFAULT (datetime('now','localtime')),
            updated_at TEXT NOT NULL DEFAULT (datetime('now','localtime'))
        );

        CREATE TABLE IF NOT EXISTS catalog_products (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name TEXT NOT NULL UNIQUE,
            cost REAL NOT NULL DEFAULT 0,
            sale_price REAL NOT NULL DEFAULT 0,
            unit TEXT NOT NULL DEFAULT '件',
            threshold REAL,
            status TEXT NOT NULL DEFAULT 'active',
            created_at TEXT NOT NULL DEFAULT (datetime('now','localtime')),
            updated_at TEXT NOT NULL DEFAULT (datetime('now','localtime'))
        );

        CREATE TABLE IF NOT EXISTS catalog_service_materials (
            service_id INTEGER NOT NULL REFERENCES catalog_services(id),
            product_name TEXT NOT NULL,
            qty REAL NOT NULL DEFAULT 1,
            PRIMARY KEY (service_id, product_name)
        );

        CREATE TABLE IF NOT EXISTS store_cards (
            id TEXT PRIMARY KEY,
            member_id TEXT NOT NULL REFERENCES store_members(id),
            name TEXT NOT NULL,
            service_id INTEGER REFERENCES catalog_services(id),
            service_name TEXT,
            total_times INTEGER NOT NULL,
            used_times INTEGER NOT NULL DEFAULT 0,
            price REAL NOT NULL DEFAULT 0,
            expire_date TEXT,
            status TEXT NOT NULL DEFAULT 'active',
            created_at TEXT NOT NULL DEFAULT (datetime('now','localtime'))
        );
        CREATE INDEX IF NOT EXISTS idx_store_cards_expire ON store_cards(expire_date, status);
        """,
        # staff before order_line_staff
        """
        CREATE TABLE IF NOT EXISTS staff_staff (
            id TEXT PRIMARY KEY,
            name TEXT NOT NULL,
            phone TEXT,
            join_date TEXT,
            base_salary REAL NOT NULL DEFAULT 0,
            fields_json TEXT NOT NULL DEFAULT '{}',
            note TEXT NOT NULL DEFAULT '',
            status TEXT NOT NULL DEFAULT 'active',
            created_at TEXT NOT NULL DEFAULT (datetime('now','localtime')),
            updated_at TEXT NOT NULL DEFAULT (datetime('now','localtime'))
        );

        CREATE TABLE IF NOT EXISTS staff_commission_rules (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            staff_id TEXT NOT NULL REFERENCES staff_staff(id),
            kind TEXT NOT NULL CHECK(kind IN ('service','product')),
            target_name TEXT,
            ratio REAL NOT NULL,
            UNIQUE(staff_id, kind, target_name)
        );

        CREATE TABLE IF NOT EXISTS staff_schedules (
            staff_id TEXT NOT NULL REFERENCES staff_staff(id),
            date TEXT NOT NULL,
            status TEXT NOT NULL DEFAULT 'work',
            slots_json TEXT,
            PRIMARY KEY (staff_id, date)
        );

        CREATE TABLE IF NOT EXISTS staff_performance (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            staff_id TEXT NOT NULL REFERENCES staff_staff(id),
            ymd TEXT NOT NULL,
            kind TEXT NOT NULL DEFAULT 'service',
            target_name TEXT NOT NULL DEFAULT '',
            amount REAL NOT NULL DEFAULT 0,
            source_appt_id TEXT,
            source_order_id TEXT,
            line_id INTEGER,
            created_at TEXT NOT NULL DEFAULT (datetime('now','localtime'))
        );
        CREATE INDEX IF NOT EXISTS idx_staff_perf_staff_ymd ON staff_performance(staff_id, ymd);
        """,
        # appointments & orders
        """
        CREATE TABLE IF NOT EXISTS store_appointments (
            id TEXT PRIMARY KEY,
            member_id TEXT NOT NULL REFERENCES store_members(id),
            service_name TEXT NOT NULL,
            technician_id TEXT NOT NULL REFERENCES staff_staff(id),
            start_dt TEXT NOT NULL,
            duration_min INTEGER NOT NULL DEFAULT 60,
            status TEXT NOT NULL DEFAULT 'scheduled',
            remark TEXT NOT NULL DEFAULT '',
            created_at TEXT NOT NULL DEFAULT (datetime('now','localtime')),
            updated_at TEXT NOT NULL DEFAULT (datetime('now','localtime'))
        );
        CREATE INDEX IF NOT EXISTS idx_store_appt_tech_dt ON store_appointments(technician_id, start_dt, status);

        CREATE TABLE IF NOT EXISTS store_orders (
            id TEXT PRIMARY KEY,
            member_id TEXT REFERENCES store_members(id),
            status TEXT NOT NULL DEFAULT 'draft',
            total_amount REAL NOT NULL DEFAULT 0,
            remark TEXT NOT NULL DEFAULT '',
            created_at TEXT NOT NULL DEFAULT (datetime('now','localtime')),
            updated_at TEXT NOT NULL DEFAULT (datetime('now','localtime'))
        );

        CREATE TABLE IF NOT EXISTS store_order_lines (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            order_id TEXT NOT NULL REFERENCES store_orders(id),
            kind TEXT NOT NULL CHECK(kind IN ('service','product')),
            ref_name TEXT NOT NULL,
            qty REAL NOT NULL DEFAULT 1,
            unit_price REAL NOT NULL DEFAULT 0,
            line_amount REAL NOT NULL DEFAULT 0
        );

        CREATE TABLE IF NOT EXISTS store_order_line_staff (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            line_id INTEGER NOT NULL REFERENCES store_order_lines(id),
            staff_id TEXT NOT NULL REFERENCES staff_staff(id),
            role TEXT NOT NULL DEFAULT 'primary',
            split_ratio REAL NOT NULL DEFAULT 1.0
        );

        CREATE TABLE IF NOT EXISTS store_payments (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            appt_id TEXT REFERENCES store_appointments(id),
            order_id TEXT REFERENCES store_orders(id),
            type TEXT NOT NULL,
            amount REAL NOT NULL DEFAULT 0,
            meta_json TEXT NOT NULL DEFAULT '{}',
            created_at TEXT NOT NULL DEFAULT (datetime('now','localtime'))
        );
        """,
        # inventory & meta & audit
        """
        CREATE TABLE IF NOT EXISTS inv_items (
            key TEXT PRIMARY KEY,
            stock_qty REAL NOT NULL DEFAULT 0,
            unit TEXT NOT NULL DEFAULT '',
            cost REAL NOT NULL DEFAULT 0,
            sale_price REAL NOT NULL DEFAULT 0,
            low_stock_threshold REAL NOT NULL DEFAULT 0,
            status TEXT NOT NULL DEFAULT 'active',
            created_at TEXT NOT NULL DEFAULT (datetime('now','localtime')),
            updated_at TEXT NOT NULL DEFAULT (datetime('now','localtime'))
        );
        CREATE INDEX IF NOT EXISTS idx_inv_items_threshold ON inv_items(low_stock_threshold);

        CREATE TABLE IF NOT EXISTS inv_logs (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            item_key TEXT NOT NULL REFERENCES inv_items(key),
            direction TEXT NOT NULL CHECK(direction IN ('in','out')),
            type TEXT NOT NULL DEFAULT '',
            quantity REAL NOT NULL,
            before_qty REAL NOT NULL,
            after_qty REAL NOT NULL,
            ref_appt_id TEXT,
            ref_order_id TEXT,
            remark TEXT NOT NULL DEFAULT '',
            created_at TEXT NOT NULL DEFAULT (datetime('now','localtime'))
        );
        CREATE INDEX IF NOT EXISTS idx_inv_logs_item_time ON inv_logs(item_key, created_at);

        CREATE TABLE IF NOT EXISTS sent_reminders (
            reminder_type TEXT NOT NULL,
            target_id TEXT NOT NULL,
            bucket TEXT NOT NULL,
            sent_at TEXT NOT NULL DEFAULT (datetime('now','localtime')),
            note TEXT NOT NULL DEFAULT '',
            UNIQUE(reminder_type, target_id, bucket)
        );
        CREATE INDEX IF NOT EXISTS idx_sent_reminders_key ON sent_reminders(reminder_type, target_id, bucket);

        CREATE TABLE IF NOT EXISTS store_meta (
            key TEXT PRIMARY KEY,
            value_json TEXT NOT NULL DEFAULT '{}'
        );

        CREATE TABLE IF NOT EXISTS store_operation_log (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            occurred_at TEXT NOT NULL,
            session_id TEXT NOT NULL DEFAULT '',
            skill TEXT NOT NULL,
            command TEXT NOT NULL,
            op TEXT NOT NULL,
            table_name TEXT NOT NULL,
            entity_id TEXT,
            request_json TEXT,
            before_json TEXT,
            after_json TEXT,
            result TEXT NOT NULL,
            error_code TEXT,
            confirmed INTEGER NOT NULL DEFAULT 0,
            duration_ms INTEGER NOT NULL DEFAULT 0
        );
        CREATE INDEX IF NOT EXISTS idx_store_operation_log_time ON store_operation_log(occurred_at);
        CREATE INDEX IF NOT EXISTS idx_store_operation_log_table ON store_operation_log(table_name, op);
        """,
    ]

REQUIRED_TABLES = [
    "store_members",
    "store_wallet",
    "store_operation_log",
    "catalog_services",
    "inv_items",
    "staff_staff",
    "store_meta",
]

SEED_DATA_TABLES = [
    "store_members",
    "catalog_services",
    "staff_staff",
    "inv_items",
    "store_meta",
]
