-- Migration: Add LinkedIn accounts for auto-posting
-- Description: Allows employees to link their LinkedIn accounts for automatic post publishing

CREATE TABLE IF NOT EXISTS linkedin_accounts (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),

    -- LinkedIn Identity
    linkedin_user_id TEXT NOT NULL,
    linkedin_vanity_name TEXT,
    linkedin_name TEXT,
    linkedin_picture TEXT,

    -- OAuth Tokens (encrypted)
    access_token TEXT NOT NULL,
    refresh_token TEXT,
    token_expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
    granted_scopes TEXT[] DEFAULT '{}',

    -- Status
    is_active BOOLEAN DEFAULT TRUE,
    last_used_at TIMESTAMP WITH TIME ZONE,
    last_error TEXT,
    last_error_at TIMESTAMP WITH TIME ZONE,

    UNIQUE(user_id)  -- Only one account per user
);

CREATE INDEX idx_linkedin_accounts_user_id ON linkedin_accounts(user_id);

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

-- Policy: Users can only see their own LinkedIn account
CREATE POLICY "Users can view own linkedin account"
    ON linkedin_accounts FOR SELECT
    USING (auth.uid() = user_id);

-- Policy: Users can insert their own LinkedIn account
CREATE POLICY "Users can insert own linkedin account"
    ON linkedin_accounts FOR INSERT
    WITH CHECK (auth.uid() = user_id);

-- Policy: Users can update their own LinkedIn account
CREATE POLICY "Users can update own linkedin account"
    ON linkedin_accounts FOR UPDATE
    USING (auth.uid() = user_id);

-- Policy: Users can delete their own LinkedIn account
CREATE POLICY "Users can delete own linkedin account"
    ON linkedin_accounts FOR DELETE
    USING (auth.uid() = user_id);

-- Function to update updated_at timestamp
CREATE OR REPLACE FUNCTION update_linkedin_accounts_updated_at()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = NOW();
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_linkedin_accounts_updated_at
    BEFORE UPDATE ON linkedin_accounts
    FOR EACH ROW
    EXECUTE FUNCTION update_linkedin_accounts_updated_at();
