import { useServiceConnection } from '../../hooks/useServiceConnection';
import { CrossRepoIssuesView, ExploreView, NotificationsView, OrganizationsView } from './DashboardViews';
import { GiteaConnection } from './GiteaConnection';
import { RepositoriesView } from './RepositoriesView';
import { RepoView } from './RepoView';
import { useGiteaLocation, useGiteaSection } from './useGiteaLocation';

// Right panel of the /gitea workspace — renders whatever the URL names.
//
// A repository route (/gitea/repo/:owner/:name/…) has no section segment at all, so it is checked first:
// `owner` being set is the signal that the URL is pointing inside a repository rather than at a section.
//
// With no instance configured every other view can only render an error, so the connection form takes over
// until there is one. The URL is left alone: once connected, whatever is already in it is what appears.

export const GiteaView = () => {
  const section = useGiteaSection();
  const { owner, repo } = useGiteaLocation();
  const { data, isLoading } = useServiceConnection('gitea');

  if (!isLoading && !data?.configured) return <GiteaConnection />;
  if (owner && repo) return <RepoView />;

  switch (section) {
    case 'issues':
      return <CrossRepoIssuesView type="issues" />;
    case 'pulls':
      return <CrossRepoIssuesView type="pulls" />;
    case 'notifications':
      return <NotificationsView />;
    case 'explore':
      return <ExploreView />;
    case 'organizations':
      return <OrganizationsView />;
    case 'connection':
      return <GiteaConnection />;
    default:
      return <RepositoriesView />;
  }
};
