import { Route, Routes } from 'react-router-dom';
import { ProtectedRoute } from './hooks/useAuth';
import Layout from './components/Layout';
import LoginPage from './pages/LoginPage';
import Dashboard from './pages/Dashboard';
import WorkflowList from './pages/WorkflowList';
import WorkflowView from './pages/WorkflowView';
import WorkflowEditor from './pages/WorkflowEditor';
import TenantList from './pages/TenantList';
import UserList from './pages/UserList';

function AppLayout({ children }: { children: React.ReactNode }) {
  return <Layout>{children}</Layout>;
}

export default function App() {
  return (
    <Routes>
      <Route path="/login" element={<LoginPage />} />

      <Route
        path="/"
        element={
          <ProtectedRoute>
            <AppLayout>
              <Dashboard />
            </AppLayout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/workflows"
        element={
          <ProtectedRoute>
            <AppLayout>
              <WorkflowList />
            </AppLayout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/workflows/new"
        element={
          <ProtectedRoute>
            <AppLayout>
              <WorkflowEditor />
            </AppLayout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/workflows/:id"
        element={
          <ProtectedRoute>
            <AppLayout>
              <WorkflowView />
            </AppLayout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/workflows/:id/edit"
        element={
          <ProtectedRoute>
            <AppLayout>
              <WorkflowEditor />
            </AppLayout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/tenants"
        element={
          <ProtectedRoute requiredRoles={['SYSTEM_ADMIN', 'TECHNICIAN']}>
            <AppLayout>
              <TenantList />
            </AppLayout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/users"
        element={
          <ProtectedRoute requiredRoles={['SYSTEM_ADMIN', 'TECHNICIAN']}>
            <AppLayout>
              <UserList />
            </AppLayout>
          </ProtectedRoute>
        }
      />
    </Routes>
  );
}
