CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(255) UNIQUE NOT NULL,
    password TEXT NOT NULL,
    created TIMESTAMPTZ,
    email VARCHAR(255),
    last_login TIMESTAMPTZ,
    owner INTEGER
);

CREATE TABLE service_categories (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    description TEXT NOT NULL,
    clicks_total INTEGER DEFAULT 0,
    total_companies INTEGER DEFAULT 0
);

CREATE TABLE companies (
    id SERIAL PRIMARY KEY,
    active BOOLEAN DEFAULT true,
    created DATE NOT NULL DEFAULT CURRENT_DATE,
    name VARCHAR(255) NOT NULL,
    address VARCHAR(255),
    town VARCHAR(255),
    state VARCHAR(2),
    phone VARCHAR(20),
    owner_name VARCHAR(255),
    owner_phone_number VARCHAR(20),
    email VARCHAR(255),
    user_id INTEGER
);

CREATE TABLE listings (
    id SERIAL PRIMARY KEY,
    company_name VARCHAR(255) NOT NULL,
    is_active BOOLEAN DEFAULT true,
    price_per_gallon DOUBLE PRECISION NOT NULL,
    price_per_gallon_cash DOUBLE PRECISION,
    note TEXT,
    minimum_order INTEGER,
    service BOOLEAN DEFAULT false,
    bio_percent INTEGER NOT NULL,
    phone VARCHAR(20),
    online_ordering VARCHAR(20) NOT NULL DEFAULT 'none',
    county_id INTEGER NOT NULL,
    user_id INTEGER NOT NULL,
    created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    last_edited TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);

-- If the table already exists, add the new columns
-- ALTER TABLE listings ADD COLUMN price_per_gallon_cash DOUBLE PRECISION;
-- ALTER TABLE listings ADD COLUMN note TEXT;
-- ALTER TABLE listings ADD COLUMN minimum_order INTEGER;
-- ALTER TABLE listings ADD COLUMN last_edited TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP;

-- If the table already exists, drop the company_id column
-- ALTER TABLE listings DROP COLUMN company_id;
