CREATE TABLE IF NOT EXISTS receipts (
    id TEXT PRIMARY KEY,
    image_path TEXT NOT NULL,
    image_width INTEGER,
    image_height INTEGER,
    items_json TEXT NOT NULL DEFAULT '[]',
    store_name TEXT,                    -- extracted from the receipt, editable
    receipt_date TEXT,                  -- YYYY-MM-DD, extracted; falls back to
                                         -- today's date if unreadable/unset
    status TEXT NOT NULL DEFAULT 'extracted',   -- extracted | grouped | done
    created_at TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS groups (
    id TEXT PRIMARY KEY,
    receipt_id TEXT NOT NULL REFERENCES receipts(id) ON DELETE CASCADE,
    name TEXT NOT NULL,
    cospend_project_id TEXT NOT NULL,
    payer_member_id TEXT NOT NULL,
    member_ids_json TEXT NOT NULL,   -- json list of cospend member ids (owers)
    item_ids_json TEXT NOT NULL,     -- json list of item ids from receipts.items_json
    status TEXT NOT NULL DEFAULT 'pending',   -- pending | submitted | failed
    share_url TEXT,
    cospend_bill_id TEXT,
    error TEXT,
    created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
