-- Phase 6: B2B Hosting Integration (Minimal Viable B2B) -- Hosting provider accounts (resellers, hosting companies) CREATE TABLE IF NOT EXISTS hosts ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), company_name VARCHAR(200) NOT NULL, contact_email VARCHAR(255) UNIQUE NOT NULL, api_key VARCHAR(64) UNIQUE NOT NULL, -- SHA-256 hash of API key wholesale_rate_usd DECIMAL(10,2) DEFAULT 6.00, -- $6/server/month default active BOOLEAN DEFAULT true, created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW() ); -- Host-provisioned licenses (tracks which licenses were provisioned by which host) CREATE TABLE IF NOT EXISTS host_licenses ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), host_id UUID NOT NULL REFERENCES hosts(id) ON DELETE CASCADE, license_id UUID NOT NULL REFERENCES licenses(id) ON DELETE CASCADE, server_identifier VARCHAR(255), -- Host's internal server ID (e.g., "rust-nyc-01") customer_email VARCHAR(255), -- End customer email (for host's records) provisioned_at TIMESTAMPTZ DEFAULT NOW(), last_seen_at TIMESTAMPTZ, -- Updated when server heartbeat received UNIQUE(host_id, license_id) ); -- Monthly billing records (for invoice generation) CREATE TABLE IF NOT EXISTS host_billing_records ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), host_id UUID NOT NULL REFERENCES hosts(id) ON DELETE CASCADE, billing_month DATE NOT NULL, -- First day of month (e.g., 2026-02-01) active_license_count INTEGER NOT NULL, wholesale_rate_usd DECIMAL(10,2) NOT NULL, total_amount_usd DECIMAL(10,2) NOT NULL, -- active_license_count * wholesale_rate_usd generated_at TIMESTAMPTZ DEFAULT NOW(), UNIQUE(host_id, billing_month) ); CREATE INDEX idx_hosts_api_key ON hosts(api_key); CREATE INDEX idx_host_licenses_host ON host_licenses(host_id); CREATE INDEX idx_host_licenses_license ON host_licenses(license_id); CREATE INDEX idx_host_billing_records_host_month ON host_billing_records(host_id, billing_month); COMMENT ON TABLE hosts IS 'Phase 6: Hosting provider accounts for B2B reseller program'; COMMENT ON TABLE host_licenses IS 'Phase 6: Tracks licenses provisioned by hosting providers'; COMMENT ON TABLE host_billing_records IS 'Phase 6: Monthly billing data for hosting providers ($6/server wholesale)';