/*
  # Add donations support
  
  1. New Tables
    - `donations`
      - `id` (uuid, primary key)
      - `user_id` (references users)
      - `content_type` (post or reel)
      - `content_id` (id of post or reel)
      - `amount` (decimal)
      - `message` (optional message with donation)
      - `created_at` (timestamp)

  2. Security
    - Enable RLS on donations table
    - Add policies for authenticated users
*/

-- Create donations table
CREATE TABLE IF NOT EXISTS donations (
  id SERIAL PRIMARY KEY,
  user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  content_type TEXT NOT NULL CHECK (content_type IN ('post', 'reel')),
  content_id INTEGER NOT NULL,
  amount DECIMAL(10,2) NOT NULL CHECK (amount > 0),
  message TEXT,
  created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);

-- Create indexes
CREATE INDEX IF NOT EXISTS donations_user_id_idx ON donations(user_id);
CREATE INDEX IF NOT EXISTS donations_content_idx ON donations(content_type, content_id);
CREATE INDEX IF NOT EXISTS donations_created_at_idx ON donations(created_at);

-- Enable RLS
ALTER TABLE donations ENABLE ROW LEVEL SECURITY;

-- Create policies
CREATE POLICY "Users can view all donations"
  ON donations FOR SELECT
  TO authenticated
  USING (true);

CREATE POLICY "Users can create donations"
  ON donations FOR INSERT
  TO authenticated
  WITH CHECK (user_id = auth.uid()::integer);