-- Add missing columns to restaurants table
ALTER TABLE restaurants 
ADD COLUMN IF NOT EXISTS phone VARCHAR(20),
ADD COLUMN IF NOT EXISTS email VARCHAR(255),
ADD COLUMN IF NOT EXISTS website VARCHAR(500),
ADD COLUMN IF NOT EXISTS updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;

-- Update created_at to have a default if it doesn't already
ALTER TABLE restaurants 
ALTER COLUMN created_at SET DEFAULT CURRENT_TIMESTAMP;

-- Create trigger to automatically update updated_at column
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ language 'plpgsql';

-- Drop trigger if exists and recreate
DROP TRIGGER IF EXISTS update_restaurants_updated_at ON restaurants;
CREATE TRIGGER update_restaurants_updated_at
    BEFORE UPDATE ON restaurants
    FOR EACH ROW
    EXECUTE FUNCTION update_updated_at_column();

-- Add comment for documentation
COMMENT ON COLUMN restaurants.phone IS 'Restaurant contact phone number';
COMMENT ON COLUMN restaurants.email IS 'Restaurant contact email address';
COMMENT ON COLUMN restaurants.website IS 'Restaurant website URL';
COMMENT ON COLUMN restaurants.updated_at IS 'Timestamp of last update';