-- Create adminusers table if it doesn't exist
CREATE TABLE IF NOT EXISTS adminusers (
  id SERIAL PRIMARY KEY,
  email VARCHAR(255) UNIQUE NOT NULL,
  name VARCHAR(255) NOT NULL,
  passwordhash TEXT NOT NULL,
  role VARCHAR(50) DEFAULT 'admin',
  createdat TIMESTAMP DEFAULT NOW(),
  lastlogin TIMESTAMP
);

-- Insert temporary admin user
-- Password: TempAdmin2024!
-- Bcrypt hash generated with 10 salt rounds
INSERT INTO adminusers (email, name, passwordhash, role, createdat) 
VALUES (
  'admin@skyartshop.com',
  'Temporary Admin',
  '$2b$10$YvK5rQE4nHjZH5tVFZ1lNu5iK7Jx/lMQXZvhGEg8sK1vF0N3wL5oG',
  'superadmin',
  NOW()
)
ON CONFLICT (email) DO UPDATE 
SET passwordhash = EXCLUDED.passwordhash,
    name = EXCLUDED.name,
    role = EXCLUDED.role;

-- Create appusers table for public users (if needed)
CREATE TABLE IF NOT EXISTS appusers (
  id SERIAL PRIMARY KEY,
  email VARCHAR(255) UNIQUE NOT NULL,
  name VARCHAR(255) NOT NULL,
  passwordhash TEXT NOT NULL,
  createdat TIMESTAMP DEFAULT NOW(),
  lastlogin TIMESTAMP
);

-- Create sessions table for express-session
CREATE TABLE IF NOT EXISTS session (
  sid VARCHAR NOT NULL COLLATE "default",
  sess JSON NOT NULL,
  expire TIMESTAMP(6) NOT NULL,
  PRIMARY KEY (sid)
);

CREATE INDEX IF NOT EXISTS IDX_session_expire ON session (expire);

SELECT 'Database setup complete!' as status;
