-- ============================================================
-- Tally AutoFill – License System Database
-- Run this once in cPanel > phpMyAdmin
-- ============================================================

CREATE TABLE IF NOT EXISTS licenses (
    id            INT AUTO_INCREMENT PRIMARY KEY,
    license_key   VARCHAR(40) NOT NULL UNIQUE,
    email         VARCHAR(100) NOT NULL,
    customer_name VARCHAR(100) NOT NULL,
    plan          ENUM('trial','monthly','yearly','lifetime') DEFAULT 'trial',
    status        ENUM('active','expired','suspended') DEFAULT 'active',
    machine_id    VARCHAR(100) DEFAULT NULL,  -- locked to first PC that activates
    razorpay_id   VARCHAR(100) DEFAULT NULL,  -- payment reference
    created_at    DATETIME DEFAULT CURRENT_TIMESTAMP,
    expires_at    DATETIME DEFAULT NULL,      -- NULL = lifetime
    last_check    DATETIME DEFAULT NULL,
    check_count   INT DEFAULT 0
);

CREATE TABLE IF NOT EXISTS activity_log (
    id          INT AUTO_INCREMENT PRIMARY KEY,
    license_key VARCHAR(40),
    machine_id  VARCHAR(100),
    action      VARCHAR(50),
    ip          VARCHAR(45),
    note        TEXT,
    created_at  DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- Example: insert a test license (delete after testing)
INSERT INTO licenses (license_key, email, customer_name, plan, status, expires_at)
VALUES ('TALLY-TEST1-ABCD-1234', 'test@example.com', 'Test User', 'monthly', 'active', DATE_ADD(NOW(), INTERVAL 30 DAY));
