'use client';

import { Badge } from '@/components/ui/Badge';
import { Task } from '@/lib/types';
import { TfsConfig } from '@/services/integrations/tfs/tfs';
import { useUserPreferences } from '@/contexts/UserPreferencesContext';

interface TaskTfsInfoProps {
  task: Task;
}

export function TaskTfsInfo({ task }: TaskTfsInfoProps) {
  const { preferences } = useUserPreferences();

  // Helper pour construire l'URL TFS
  const getTfsPullRequestUrl = (
    pullRequestId: number,
    project: string,
    repository: string
  ): string => {
    const organizationUrl = (preferences.tfsConfig as TfsConfig)
      ?.organizationUrl;
    if (!organizationUrl || !pullRequestId || !project || !repository)
      return '';
    return `${organizationUrl}/${project}/_git/${repository}/pullrequest/${pullRequestId}`;
  };

  if (task.source !== 'tfs' || !task.tfsPullRequestId) {
    return null;
  }

  return (
    <div className="space-y-3">
      <label className="block text-sm font-mono font-medium text-[var(--muted-foreground)] uppercase tracking-wider">
        TFS / Azure DevOps
      </label>

      <div className="flex items-center gap-3 flex-wrap">
        {preferences.tfsConfig &&
        (preferences.tfsConfig as TfsConfig).organizationUrl ? (
          <a
            href={getTfsPullRequestUrl(
              task.tfsPullRequestId,
              task.tfsProject || '',
              task.tfsRepository || ''
            )}
            target="_blank"
            rel="noopener noreferrer"
            className="hover:scale-105 transition-transform inline-flex"
          >
            <Badge
              variant="outline"
              size="sm"
              className="hover:bg-orange-500/10 hover:border-orange-400/50 cursor-pointer"
            >
              PR-{task.tfsPullRequestId}
            </Badge>
          </a>
        ) : (
          <Badge variant="outline" size="sm">
            PR-{task.tfsPullRequestId}
          </Badge>
        )}

        {task.tfsRepository && (
          <Badge
            variant="outline"
            size="sm"
            className="text-orange-400 border-orange-400/30"
          >
            {task.tfsRepository}
          </Badge>
        )}

        {task.tfsProject && (
          <Badge
            variant="outline"
            size="sm"
            className="text-blue-400 border-blue-400/30"
          >
            {task.tfsProject}
          </Badge>
        )}

        {task.tfsSourceBranch && (
          <Badge
            variant="outline"
            size="sm"
            className="text-yellow-400 border-yellow-400/30"
          >
            {task.tfsSourceBranch.replace('refs/heads/', '')}
          </Badge>
        )}

        {task.tfsTargetBranch &&
          task.tfsTargetBranch !== task.tfsSourceBranch && (
            <Badge
              variant="outline"
              size="sm"
              className="text-green-400 border-green-400/30"
            >
              → {task.tfsTargetBranch.replace('refs/heads/', '')}
            </Badge>
          )}
      </div>
    </div>
  );
}
