-- Create the availabilities table
CREATE TABLE IF NOT EXISTS availabilities (
    id SERIAL PRIMARY KEY,
    user_id uuid NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
    availabilities JSONB NOT NULL DEFAULT '{}'::jsonb,
    created_at timestamptz NOT NULL DEFAULT now(),
    updated_at timestamptz NOT NULL DEFAULT now()
);

-- Create an index for faster lookups by user_id
CREATE INDEX IF NOT EXISTS idx_availabilities_user_id ON availabilities(user_id);

-- Add unique constraint on user_id to ensure one availability record per user
ALTER TABLE availabilities ADD CONSTRAINT unique_user_availabilities UNIQUE (user_id);


-- Add trigger to update updated_at timestamp
CREATE TRIGGER update_availabilities_updated_at
    BEFORE UPDATE ON availabilities
    FOR EACH ROW
    EXECUTE FUNCTION update_updated_at_column();

-- Enable Row Level Security
ALTER TABLE availabilities ENABLE ROW LEVEL SECURITY;

-- Policy to allow users to view their own availabilities
CREATE POLICY "Users can view their own availabilities" ON availabilities
    FOR SELECT
    TO authenticated
    USING (user_id = auth.uid());

-- Policy to allow users to insert their own availabilities
CREATE POLICY "Users can insert their own availabilities" ON availabilities
    FOR INSERT
    TO authenticated
    WITH CHECK (user_id = auth.uid());

-- Policy to allow users to update their own availabilities
CREATE POLICY "Users can update their own availabilities" ON availabilities
    FOR UPDATE
    TO authenticated
    USING (user_id = auth.uid())
    WITH CHECK (user_id = auth.uid());

-- Policy to allow users to delete their own availabilities
CREATE POLICY "Users can delete their own availabilities" ON availabilities
    FOR DELETE
    TO authenticated
    USING (user_id = auth.uid());

-- Add helpful comments
COMMENT ON TABLE availabilities IS 
    'User availability settings with Row Level Security';

COMMENT ON COLUMN availabilities.id IS 
    'Primary key: auto-incrementing integer';

COMMENT ON COLUMN availabilities.user_id IS 
    'Foreign key reference to auth.users(id)';

COMMENT ON COLUMN availabilities.availabilities IS 
    'JSONB object containing availability settings for each day (0-6, where 0 is Monday). Each day has enabled status and time ranges.';

-- Rename the availabilities column to availability_data for clarity
ALTER TABLE availabilities RENAME COLUMN availabilities TO availability_data;

-- Update the comment for the renamed column
COMMENT ON COLUMN availabilities.availability_data IS 
    'JSONB object containing availability settings for each day (0-6, where 0 is Monday). Each day has enabled status and time ranges.';

-- Add exceptions column for date-specific availability overrides
ALTER TABLE availabilities ADD COLUMN exceptions JSONB DEFAULT '[]';

-- Add comment for the exceptions column
COMMENT ON COLUMN availabilities.exceptions IS 
    'JSONB object containing date-specific availability exceptions that override regular availability settings. Keys are ISO date strings (YYYY-MM-DD), values contain exception type and optional time ranges.';
