-- Templates table for storing import templates
CREATE TABLE IF NOT EXISTS templates (
    id SERIAL PRIMARY KEY,
    company TEXT NOT NULL,
    product_type TEXT NOT NULL,
    supplier TEXT,
    msrp DECIMAL(10,2),
    cost_each DECIMAL(10,2),
    qty_per_unit INTEGER,
    case_qty INTEGER,
    hts_code TEXT,
    description TEXT,
    weight DECIMAL(10,2),
    length DECIMAL(10,2),
    width DECIMAL(10,2),
    height DECIMAL(10,2),
    tax_cat TEXT,
    size_cat TEXT,
    categories TEXT[],
    ship_restrictions TEXT[],
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(company, product_type)
);

-- Function to update the updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ language 'plpgsql';

-- Trigger to automatically update the updated_at column
CREATE TRIGGER update_templates_updated_at
    BEFORE UPDATE ON templates
    FOR EACH ROW
    EXECUTE FUNCTION update_updated_at_column(); 