package db import ( "database/sql" "fmt" "os" "path/filepath" "strings" _ "github.com/glebarez/sqlite" ) const schema = ` CREATE TABLE IF NOT EXISTS accounts ( account_id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT UNIQUE NOT NULL, name TEXT NOT NULL, password TEXT NOT NULL, created_at INTEGER NOT NULL ); CREATE TABLE IF NOT EXISTS sessions ( session_id TEXT PRIMARY KEY, account_id INTEGER NOT NULL, expires INTEGER NOT NULL, FOREIGN KEY (account_id) REFERENCES accounts(account_id) ); CREATE TABLE IF NOT EXISTS clients ( client_id INTEGER PRIMARY KEY AUTOINCREMENT, account_id INTEGER NOT NULL, name TEXT NOT NULL, phone TEXT, email TEXT, address TEXT, notes TEXT, whatsapp_number TEXT, whatsapp_connected INTEGER DEFAULT 0, created_at INTEGER NOT NULL, whatsapp_jid TEXT, FOREIGN KEY (account_id) REFERENCES accounts(account_id) ); CREATE TABLE IF NOT EXISTS customers ( customer_id INTEGER PRIMARY KEY AUTOINCREMENT, client_id INTEGER NOT NULL, name TEXT NOT NULL, phone TEXT, birth_date TEXT, instagram TEXT, created_at INTEGER NOT NULL, FOREIGN KEY (client_id) REFERENCES clients(client_id) ); CREATE TABLE IF NOT EXISTS services ( service_id INTEGER PRIMARY KEY AUTOINCREMENT, client_id INTEGER NOT NULL, name TEXT NOT NULL, price REAL, description TEXT, duration TEXT, created_at INTEGER NOT NULL, FOREIGN KEY (client_id) REFERENCES clients(client_id) ); CREATE TABLE IF NOT EXISTS scheduling ( schedule_id INTEGER PRIMARY KEY AUTOINCREMENT, client_id INTEGER NOT NULL, customer_id INTEGER NOT NULL, service_id INTEGER NOT NULL, plan_date TEXT, time TEXT, status TEXT DEFAULT 'pending', created_at INTEGER NOT NULL, FOREIGN KEY (client_id) REFERENCES clients(client_id), FOREIGN KEY (customer_id) REFERENCES customers(customer_id), FOREIGN KEY (service_id) REFERENCES services(service_id) ); CREATE TABLE IF NOT EXISTS payments ( payment_id INTEGER PRIMARY KEY AUTOINCREMENT, client_id INTEGER NOT NULL, customer_id INTEGER NOT NULL, schedule_id INTEGER, has_paid INTEGER DEFAULT 0, amount REAL, payment_date TEXT, payment_method TEXT, created_at INTEGER NOT NULL, FOREIGN KEY (client_id) REFERENCES clients(client_id), FOREIGN KEY (customer_id) REFERENCES customers(customer_id), FOREIGN KEY (schedule_id) REFERENCES scheduling(schedule_id) ); CREATE TABLE IF NOT EXISTS questions ( question_id INTEGER PRIMARY KEY AUTOINCREMENT, client_id INTEGER NOT NULL, customer_id INTEGER NOT NULL, question TEXT NOT NULL, timestamp INTEGER NOT NULL, status TEXT DEFAULT 'pending', created_at INTEGER NOT NULL, FOREIGN KEY (client_id) REFERENCES clients(client_id), FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ); CREATE TABLE IF NOT EXISTS answers ( answer_id INTEGER PRIMARY KEY AUTOINCREMENT, client_id INTEGER NOT NULL, question_id INTEGER NOT NULL, answer TEXT NOT NULL, timestamp INTEGER NOT NULL, status TEXT DEFAULT 'active', created_at INTEGER NOT NULL, FOREIGN KEY (client_id) REFERENCES clients(client_id), FOREIGN KEY (question_id) REFERENCES questions(question_id) ); ` var dbPath = "data/go-crm.db" func Init(path string) (*sql.DB, error) { if path != "" { dbPath = path } dir := filepath.Dir(dbPath) if err := os.MkdirAll(dir, 0755); err != nil { return nil, fmt.Errorf("failed to create data directory: %w", err) } database, err := sql.Open("sqlite", dbPath) if err != nil { return nil, fmt.Errorf("failed to open database: %w", err) } if err := database.Ping(); err != nil { return nil, fmt.Errorf("failed to ping database: %w", err) } if _, err := database.Exec(schema); err != nil { return nil, fmt.Errorf("failed to create schema: %w", err) } if err := migrate(database); err != nil { return nil, fmt.Errorf("failed to migrate: %w", err) } return database, nil } func migrate(db *sql.DB) error { _, err := db.Exec("ALTER TABLE clients ADD COLUMN account_id INTEGER") if err != nil { if !strings.Contains(err.Error(), "duplicate column name") { return err } } _, err = db.Exec("ALTER TABLE clients ADD COLUMN whatsapp_number TEXT") if err != nil { if !strings.Contains(err.Error(), "duplicate column name") { return err } } _, err = db.Exec("ALTER TABLE clients ADD COLUMN whatsapp_connected INTEGER DEFAULT 0") if err != nil { if !strings.Contains(err.Error(), "duplicate column name") { return err } } _, err = db.Exec("ALTER TABLE clients ADD COLUMN whatsapp_jid TEXT") if err != nil { if !strings.Contains(err.Error(), "duplicate column name") { return err } } return nil }