import { createAction, Property } from '@activepieces/pieces-framework';
import { blueskyAuth, BlueSkyAuthType } from '../common/auth';
import { createBlueskyAgent } from '../common/client';
import { parseBlueskyUrl } from '../common/props';


export const likePost = createAction({
  auth: blueskyAuth,
  name: 'likePost',
  displayName: 'Like Post',
  description: 'Like a post on Bluesky',
  audience: 'both',
  aiMetadata: {
    description: 'Adds a like from the authenticated account to a specific Bluesky post, identified either by an AT-URI / bsky.app post URL or by selecting from the recent timeline. Use to register approval of a post. Not idempotent — each call creates a separate like record.',
    idempotent: false,
  },
  props: {
    selectionMethod: Property.StaticDropdown({
      displayName: 'Select Method',
      description: 'How to choose the post',
      required: true,
      defaultValue: 'timeline',
      options: {
        options: [
          { label: 'From my timeline', value: 'timeline' },
          { label: 'Enter URL manually', value: 'manual' },
        ],
      },
    }),
    
    postSelection: Property.Dropdown({
      auth: blueskyAuth,
      displayName: 'Select Post',
      description: 'Choose from your recent timeline posts (only when "From my timeline" is selected above)',
      required: false,
      refreshers: ['auth'],
      options: async ({ auth }) => {
        try {
          if (!auth) return { options: [] };
            const agent = await createBlueskyAgent(auth.props);
          const timeline = await agent.getTimeline({ limit: 50 });
          
          return {
            options: timeline.data.feed.map(item => ({
              label: `@${item.post.author.handle}: ${item.post.record['text'] ? String(item.post.record['text']).substring(0, 80) : 'Media post'}${String(item.post.record['text'] || '').length > 80 ? '...' : ''} (${new Date(item.post.indexedAt).toLocaleDateString()})`,
              value: item.post.uri
            }))
          };
        } catch (error) {
          return { 
            options: [{ label: 'Error loading posts', value: '' }] 
          };
        }
      }
    }),

    postUrl: Property.ShortText({
      displayName: 'Post URL',
      description: 'Paste the Bluesky post URL',
      required: false,
    }),
  },
  async run({ auth, propsValue }) {
    const { selectionMethod, postSelection, postUrl } = propsValue;

    let postUri: string;
    
    if (selectionMethod === 'timeline') {
      if (!postSelection) {
        throw new Error('Please select a post from your timeline dropdown');
      }
      postUri = postSelection as string;
    } else if (selectionMethod === 'manual') {
      if (!postUrl || !postUrl.trim()) {
        throw new Error('Post URL is required when using manual entry method');
      }
      
      try {
        const agent = await createBlueskyAgent(auth.props);
        postUri = await parseBlueskyUrl(postUrl.trim(), agent);
      } catch (error) {
        throw new Error(`Invalid post URL: ${error instanceof Error ? error.message : 'Unknown error'}`);
      }
    } else {
      throw new Error('Please select a post selection method');
    }

    try {
      const agent = await createBlueskyAgent(auth.props);
      
      const postsResponse = await agent.getPosts({ uris: [postUri] });
      
      if (!postsResponse.data.posts || postsResponse.data.posts.length === 0) {
        throw new Error('Post not found');
      }
      
      const post = postsResponse.data.posts[0];
      
      const response = await agent.like(postUri, post.cid);

      return {
        success: true,
        likeUri: response.uri,
        likeCid: response.cid,
        postUri: postUri,
        postCid: post.cid,
        postAuthor: post.author.handle,
        postText: post.record['text'] ? String(post.record['text']).substring(0, 100) + (String(post.record['text']).length > 100 ? '...' : '') : 'No text available',
        selectionMethod: selectionMethod,
        likedAt: new Date().toISOString(),
      };
    } catch (error) {
      if (error instanceof Error) {
        if (error.message.includes('Post not found')) {
          throw new Error('Post not found. Please check the URL and try again.');
        }
        if (error.message.includes('Invalid post URL format')) {
          throw new Error('Invalid post URL format. Please use a valid Bluesky post URL or AT-URI.');
        }
        if (error.message.includes('Authentication')) {
          throw new Error('Authentication failed. Please check your credentials.');
        }
        throw new Error(`Failed to like post: ${error.message}`);
      }
      throw new Error('Failed to like post: Unknown error occurred');
    }
  },
});
