import React, { useMemo, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { ArrowLeft, Building2, Copy, Eye, EyeOff, KeyRound, MapPin, Pencil, ShieldCheck, Trash2, UserPlus } from 'lucide-react';
import {
Alert, Breadcrumb, BreadcrumbItem, BreadcrumbLink, BreadcrumbList, BreadcrumbPage,
BreadcrumbSeparator, Button, DataTable, EmptyState, Field, Input, PageHeader, Stack,
StatusBadge, Surface, Switch,
} from '@/components/ds';
import { inputVariants } from '@/components/ui/input';
import { useAuth } from '@/lib/AuthContext';
import { useOnboardClient, useOnboardedClients, useOnboardingCities } from '@/lib/doormileHooks';
import AddressAutocomplete from '@/components/doormile/AddressAutocomplete';
import { EditClientModal, RemoveClientModal } from './ClientLoginDialogs';
import {
addressFromPlace, addressPayload, canOnboardClients, generateClientPassword, hasMapLocation,
onboardingUnavailableMessage, validateOnboarding,
} from '@/lib/clientOnboarding';
/**
* Client onboarding — register a new client and give them a console login.
*
* One submit creates, in a single server transaction, the tenant (the client
* company), its console login (doormile_auth, scoped to that tenant) and its
* appusers row. The client then signs in at the normal console login and sees
* only their own data.
*
* Restricted to the onboarding owner login (admin@doormile.com). The server
* enforces that; this page only hides itself from everyone else.
*/
const EMPTY = {
companyname: '',
contactname: '',
email: '',
phone: '',
applocationid: '',
password: '',
confirm: '',
requiredeliveryotp: false,
// Main address, saved as the client's primary location. Coordinates come
// only from picking a suggestion.
address: '',
city: '',
state: '',
pincode: '',
latitude: '',
longitude: '',
};
const SERVER_FIELD = [
['company name', 'companyname'],
['contact person', 'contactname'],
['email', 'email'],
['mobile', 'phone'],
['password', 'password'],
['operating city', 'applocationid'],
['pincode', 'pincode'],
['address', 'address'],
['map location', 'address'],
];
/** Put a server message under the field it is about, when it names one. */
function serverFieldOf(message) {
const m = String(message || '').toLowerCase();
const hit = SERVER_FIELD.find(([needle]) => m.includes(needle));
return hit ? hit[1] : null;
}
function OnboardedList() {
const { data = [], isLoading } = useOnboardedClients();
const [editing, setEditing] = useState(null);
const [removing, setRemoving] = useState(null);
// Rows without an authid come from a backend older than Edit/Delete (it has
// neither the field nor the PUT/DELETE routes). Never send /clients/undefined:
// disable the actions and say what to do instead.
const backendTooOld = data.length > 0 && data.some((r) => r.authid == null);
const columns = [
{ key: 'tenantname', header: 'Client', accessor: (r) => r.tenantname },
{ key: 'loginemail', header: 'Console login', accessor: (r) => r.loginemail },
{ key: 'primarycontact', header: 'Contact', accessor: (r) => r.primarycontact || '—' },
{
key: 'address',
header: 'Address',
accessor: (r) => (r.address ? [r.address, r.pincode].filter(Boolean).join(' · ') : '—'),
},
{ key: 'status', header: 'Status', cell: (r) =>
Console login {created.login.email} is ready. They sign in at the usual console login and see only their own orders.
This is the only time the password is shown — share it with the client securely.