-- ============================================================
-- LeadYou CRM — Database Schema
-- Run this once in your MySQL database
-- ============================================================

CREATE DATABASE IF NOT EXISTS leadyou CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE leadyou;

-- Leads table
CREATE TABLE IF NOT EXISTS leads (
    id         INT AUTO_INCREMENT PRIMARY KEY,
    name       VARCHAR(100) NOT NULL,
    email      VARCHAR(150) NOT NULL,
    phone      VARCHAR(30),
    company    VARCHAR(100),
    status     ENUM('New','Contacted','Qualified','Proposal','Won','Lost') DEFAULT 'New',
    notes      TEXT,
    created_by INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL
);

-- Indexes for performance
CREATE INDEX idx_leads_status     ON leads(status);
CREATE INDEX idx_leads_created_at ON leads(created_at);
CREATE INDEX idx_leads_email      ON leads(email);
