-- ============================================================
--  Vehicle Verify — Admin Module Migration v2 (نقل الملكية + تحسينات)
--  phpMyAdmin: Database -> Import -> schema_admin_v2.sql
--  يُستورد بعد schema_admin.sql
-- ============================================================

-- جدول سجل نقل ملكية المركبات
CREATE TABLE IF NOT EXISTS ownership_transfers (
    id             INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    vehicle_id     INT UNSIGNED DEFAULT NULL,
    plate_number   VARCHAR(30)  DEFAULT NULL,
    from_owner_id  INT UNSIGNED DEFAULT NULL,
    to_owner_id    INT UNSIGNED DEFAULT NULL,
    performed_by   INT UNSIGNED DEFAULT NULL,
    created_at     TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    KEY idx_tr_vehicle (vehicle_id),
    KEY idx_tr_created (created_at),
    CONSTRAINT fk_tr_vehicle FOREIGN KEY (vehicle_id)    REFERENCES vehicles(id) ON DELETE SET NULL,
    CONSTRAINT fk_tr_from    FOREIGN KEY (from_owner_id) REFERENCES users(id)    ON DELETE SET NULL,
    CONSTRAINT fk_tr_to      FOREIGN KEY (to_owner_id)   REFERENCES users(id)    ON DELETE SET NULL,
    CONSTRAINT fk_tr_by      FOREIGN KEY (performed_by)  REFERENCES users(id)    ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
