'use client';

import { useEffect, useMemo, useState } from 'react';

type PermissionKey = 'view' | 'edit' | 'delete' | 'manage';
type RoleId = string;
type PermissionValue = boolean | null;

type PermissionRow = {
  id: string;
  label: string;
  icon: string;
  description: string;
  values: Record<PermissionKey, PermissionValue>;
};

type RoleDefinition = {
  id: RoleId;
  name: string;
  badge: string;
  tone: 'primary' | 'secondary' | 'tertiary';
  summary: string;
  usersAssigned: number;
  icon: string;
  permissionRows: PermissionRow[];
};

function cloneRoleSet(roles: RoleDefinition[]) {
  return roles.map((role) => ({
    ...role,
    permissionRows: role.permissionRows.map((row) => ({
      ...row,
      values: { ...row.values },
    })),
  }));
}

function roleBadge(role: RoleDefinition, isEditing: boolean, labels: Record<string, string>) {
  if (isEditing) {
    return { label: labels.editingNow, tone: 'editing' as const };
  }

  return { label: role.badge, tone: role.tone };
}

type AuditHighlight = {
  id: string;
  icon: string;
  title: string;
  description: string;
  time: string;
};

type Props = {
  initialRoles: RoleDefinition[];
  initialAuditHighlights: AuditHighlight[];
  labels: Record<string, string>;
};

export function RolesPermissionsBoard({ initialRoles, initialAuditHighlights, labels }: Props) {
  const permissionColumns: Array<{ key: PermissionKey; label: string }> = [
    { key: 'view', label: labels.view },
    { key: 'edit', label: labels.createEdit },
    { key: 'delete', label: labels.delete },
    { key: 'manage', label: labels.manageAll },
  ];
  const [roles, setRoles] = useState<RoleDefinition[]>(() => cloneRoleSet(initialRoles));
  const [savedRoles, setSavedRoles] = useState<RoleDefinition[]>(() => cloneRoleSet(initialRoles));
  const [selectedRoleId, setSelectedRoleId] = useState<RoleId>(initialRoles[0]?.id || '');
  const [editingRoleId, setEditingRoleId] = useState<RoleId | null>(null);
  const [saveMessage, setSaveMessage] = useState('');
  const [isCreateOpen, setIsCreateOpen] = useState(false);
  const [newRoleName, setNewRoleName] = useState('');
  const [newRoleSummary, setNewRoleSummary] = useState('');
  const [isSaving, setIsSaving] = useState(false);

  const selectedRole = useMemo(
    () => roles.find((role) => role.id === selectedRoleId) ?? roles[0],
    [roles, selectedRoleId],
  );

  const isEditingSelectedRole = selectedRole ? editingRoleId === selectedRole.id : false;

  useEffect(() => {
    const nextRoles = cloneRoleSet(initialRoles);
    setRoles(nextRoles);
    setSavedRoles(nextRoles);
    setSelectedRoleId((current) =>
      nextRoles.some((role) => role.id === current) ? current : nextRoles[0]?.id || '',
    );
    setEditingRoleId((current) =>
      nextRoles.some((role) => role.id === current) ? current : null,
    );
  }, [initialRoles]);

  function updatePermission(rowId: string, permission: PermissionKey, nextValue: boolean) {
    if (!isEditingSelectedRole) {
      return;
    }

    setRoles((currentRoles) =>
      currentRoles.map((role) =>
        role.id !== selectedRoleId
          ? role
          : {
              ...role,
              permissionRows: role.permissionRows.map((row) =>
                row.id === rowId ? { ...row, values: { ...row.values, [permission]: nextValue } } : row,
              ),
            },
      ),
    );
    setSaveMessage('');
  }

  function beginEdit(roleId: RoleId) {
    setSelectedRoleId(roleId);
    setEditingRoleId(roleId);
    setSaveMessage('');
  }

  function resetChanges() {
    setRoles(cloneRoleSet(savedRoles));
    setEditingRoleId(null);
    setSaveMessage(labels.changesDiscarded);
  }

  async function saveChanges() {
    if (!selectedRole) {
      return;
    }

    setIsSaving(true);
    try {
      const response = await fetch(`/api/roles/${selectedRole.id}`, {
        method: 'PATCH',
        credentials: 'include',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          permissions: selectedRole.permissionRows,
          summary: selectedRole.summary,
          badge: selectedRole.badge,
          tone: selectedRole.tone,
          icon: selectedRole.icon,
        }),
      });

      const payload = await response.json();
      if (!response.ok) {
        setSaveMessage(payload.message || labels.failedSave);
        return;
      }

      setRoles((current) =>
        current.map((role) =>
          role.id === payload.id
            ? {
                ...role,
                permissionRows: payload.permissions,
                usersAssigned: payload.usersAssigned,
                summary: payload.summary,
                badge: payload.badge,
                tone: payload.tone,
                icon: payload.icon,
              }
            : role,
        ),
      );
      setSavedRoles((current) =>
        current.map((role) =>
          role.id === payload.id
            ? {
                ...role,
                permissionRows: payload.permissions,
                usersAssigned: payload.usersAssigned,
                summary: payload.summary,
                badge: payload.badge,
                tone: payload.tone,
                icon: payload.icon,
              }
            : role,
        ),
      );
      setEditingRoleId(null);
      setSaveMessage(`${labels.permissionSavedPrefix} ${payload.name} ${labels.permissionSavedSuffix}`);
    } finally {
      setIsSaving(false);
    }
  }

  async function createRole() {
    const trimmedName = newRoleName.trim();
    const trimmedSummary = newRoleSummary.trim();

    if (!trimmedName) {
      setSaveMessage(labels.roleNameRequired);
      return;
    }

    const id =
      trimmedName.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)/g, '') || `role-${Date.now()}`;

    const nextRole: RoleDefinition = {
      id,
      name: trimmedName,
      badge: labels.custom,
      tone: 'secondary',
      summary: trimmedSummary || labels.customSummary,
      usersAssigned: 0,
      icon: 'verified_user',
      permissionRows: [
        { id: 'campaigns', label: 'Manage Campaigns', icon: 'campaign', description: 'Broadcasts and outbound campaign controls.', values: { view: true, edit: false, delete: false, manage: false } },
        { id: 'analytics', label: 'View Analytics', icon: 'monitoring', description: 'KPI, trends, and performance dashboards.', values: { view: true, edit: null, delete: null, manage: false } },
        { id: 'settings', label: 'Edit Settings', icon: 'settings', description: 'Providers, secrets, and environment-facing settings.', values: { view: false, edit: false, delete: false, manage: false } },
        { id: 'billing', label: 'Billing & Invoices', icon: 'payments', description: 'Plan usage, invoices, and billing visibility.', values: { view: false, edit: null, delete: null, manage: false } },
      ],
    };

    setIsSaving(true);
    try {
      const response = await fetch('/api/roles', {
        method: 'POST',
        credentials: 'include',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          name: trimmedName,
          summary: trimmedSummary || labels.customSummary,
          badge: labels.custom,
          tone: 'secondary',
          icon: 'verified_user',
          permissions: nextRole.permissionRows,
        }),
      });
      const payload = await response.json();
      if (!response.ok) {
        setSaveMessage(payload.message || labels.failedCreate);
        return;
      }

      const createdRole = {
        id: payload.id,
        name: payload.name,
        badge: payload.badge,
        tone: payload.tone,
        summary: payload.summary,
        usersAssigned: payload.usersAssigned,
        icon: payload.icon,
        permissionRows: payload.permissions,
      } satisfies RoleDefinition;

      const nextRoles = [createdRole, ...cloneRoleSet(roles)];
      setRoles(nextRoles);
      setSavedRoles(cloneRoleSet(nextRoles));
      setSelectedRoleId(payload.id);
      setEditingRoleId(payload.id);
      setIsCreateOpen(false);
      setNewRoleName('');
      setNewRoleSummary('');
      setSaveMessage(`${labels.roleCreatedPrefix} ${payload.name} ${labels.roleCreatedSuffix}`);
    } finally {
      setIsSaving(false);
    }
  }

  function localizePermissionRow(row: PermissionRow) {
    return {
      ...row,
      label: labels[`permission_${row.id}_label`] || row.label,
      description: labels[`permission_${row.id}_description`] || row.description,
    };
  }

  if (!selectedRole) {
    return (
      <section className="permission-matrix-card">
        <div className="permission-matrix-head">
          <div>
            <p className="card-kicker">{labels.roles}</p>
            <h3>{labels.noRoles}</h3>
            <p className="permission-matrix-copy">
              {labels.noRolesCopy}
            </p>
          </div>
          <button
            type="button"
            className="role-action-button"
            onClick={() => setIsCreateOpen(true)}
          >
            <span className="material-symbols-outlined">add_moderator</span>
            {labels.createRole}
          </button>
        </div>
      </section>
    );
  }

  return (
    <>
      <section className="page-section">
        <div className="header-actions">
          <button
            type="button"
            className="roles-create-button"
            onClick={() => {
              setIsCreateOpen((current) => !current);
              setSaveMessage('');
            }}
          >
            <span className="material-symbols-outlined">{isCreateOpen ? 'close' : 'add_moderator'}</span>
            {isCreateOpen ? labels.hideRoleForm : labels.createNewRole}
          </button>
        </div>
      </section>

      {isCreateOpen ? (
        <section className="roles-create-panel">
          <div className="roles-create-panel-head">
            <div>
              <p className="card-kicker">{labels.newRole}</p>
              <h3>{labels.createProfile}</h3>
              <p className="roles-create-copy">{labels.createProfileCopy}</p>
            </div>
          </div>
          <div className="roles-create-form">
            <label className="roles-create-field">
              <span>{labels.roleName}</span>
              <input type="text" value={newRoleName} onChange={(event) => setNewRoleName(event.target.value)} placeholder={labels.roleNamePlaceholder} />
              <small>{labels.roleNameHelp}</small>
            </label>
            <label className="roles-create-field">
              <span>{labels.summary}</span>
              <textarea value={newRoleSummary} onChange={(event) => setNewRoleSummary(event.target.value)} placeholder={labels.summaryPlaceholder} rows={3} />
              <small>{labels.summaryHelp}</small>
            </label>
          </div>
          <div className="button-row roles-create-actions">
            <button type="button" className="secondary-button role-action-button" onClick={() => {
              setIsCreateOpen(false);
              setNewRoleName('');
              setNewRoleSummary('');
            }}>
              {labels.cancel}
            </button>
            <button type="button" className="role-action-button" onClick={createRole} disabled={isSaving}>
              {labels.saveRole}
            </button>
          </div>
        </section>
      ) : null}

      <section className="roles-card-grid">
        {roles.map((role) => {
          const badge = roleBadge(role, editingRoleId === role.id, labels);
          const isSelected = role.id === selectedRoleId;

          return (
            <article
              key={role.id}
              className={isSelected ? 'role-card is-selected' : 'role-card'}
              onClick={() => {
                setSelectedRoleId(role.id);
                setSaveMessage('');
              }}
              aria-selected={isSelected}
            >
              <div className="role-card-head">
                <div className={`role-icon tone-${role.tone}`}>
                  <span className="material-symbols-outlined">{role.icon}</span>
                </div>
                <span className={`role-badge tone-${badge.tone}`}>{badge.label}</span>
              </div>
              <h2>{role.name}</h2>
              <p>{role.summary}</p>
              <div className="role-card-meta">
                <span className="material-symbols-outlined">group</span>
                {role.usersAssigned} {labels.usersAssigned}
              </div>
              <div className="role-card-actions">
                <button
                  type="button"
                  className={editingRoleId === role.id ? 'role-inline-button is-active' : 'role-inline-button'}
                  onClick={(event) => {
                    event.stopPropagation();
                    beginEdit(role.id);
                  }}
                >
                  <span className="material-symbols-outlined">edit</span>
                  {editingRoleId === role.id ? labels.editing : labels.edit}
                </button>
              </div>
            </article>
          );
        })}
      </section>

      <section className="permission-matrix-card">
        <div className="permission-matrix-head">
          <div>
            <p className="card-kicker">{labels.permissionMatrix}</p>
            <h3>
              {isEditingSelectedRole ? labels.editing : labels.viewing} <span>{selectedRole.name}</span>
            </h3>
            <p className="permission-matrix-copy">
              {isEditingSelectedRole
                ? labels.toggleCopy
                : labels.readonlyCopy}
            </p>
          </div>
          <div className="button-row">
            {isEditingSelectedRole ? (
              <>
                <button type="button" className="secondary-button role-action-button" onClick={resetChanges} disabled={isSaving}>
                  {labels.discard}
                </button>
                <button type="button" className="role-action-button" onClick={saveChanges} disabled={isSaving}>
                  {labels.saveChanges}
                </button>
              </>
            ) : (
              <button type="button" className="role-action-button" onClick={() => beginEdit(selectedRole.id)}>
                <span className="material-symbols-outlined">edit</span>
                {labels.editRole}
              </button>
            )}
          </div>
        </div>
        {saveMessage ? <p className="roles-feedback">{saveMessage}</p> : null}
        <div className="permission-table-wrap">
          <table className="permission-table">
            <thead>
              <tr>
                <th>{labels.modulePermission}</th>
                {permissionColumns.map((column) => (
                  <th key={column.key}>{column.label}</th>
                ))}
              </tr>
            </thead>
            <tbody>
              {selectedRole.permissionRows.map((sourceRow) => {
                const row = localizePermissionRow(sourceRow);
                return (
                <tr key={row.id}>
                  <td>
                    <div className="permission-label">
                      <span className="material-symbols-outlined">{row.icon}</span>
                      <div>
                        <strong>{row.label}</strong>
                        <small>{row.description}</small>
                      </div>
                    </div>
                  </td>
                  {permissionColumns.map((column) => {
                    const value = row.values[column.key];
                    if (value === null) {
                      return (
                        <td key={column.key} className="permission-unavailable">
                          —
                        </td>
                      );
                    }

                    if (!isEditingSelectedRole) {
                      return (
                        <td key={column.key}>
                          <span className={value ? 'permission-indicator is-on' : 'permission-indicator is-off'}>
                            {value ? labels.on : labels.off}
                          </span>
                        </td>
                      );
                    }

                    return (
                      <td key={column.key}>
                        <label className="toggle-switch">
                          <input type="checkbox" checked={value} onChange={(event) => updatePermission(row.id, column.key, event.target.checked)} />
                          <span className="toggle-track">
                            <span className="toggle-thumb" />
                          </span>
                        </label>
                      </td>
                    );
                  })}
                </tr>
                );
              })}
            </tbody>
          </table>
        </div>
      </section>

      <section className="roles-bottom-grid">
        <article className="surface-card">
          <div className="card-head">
            <div>
              <p className="card-kicker">{labels.auditTrail}</p>
              <h3 className="roles-section-title">{labels.recentChanges}</h3>
            </div>
            <a href="/dashboard/settings/audit-trail" className="roles-inline-link">
              {labels.viewAll}
            </a>
          </div>
          <div className="roles-audit-list">
            {initialAuditHighlights.map((entry) => (
              <div key={entry.id} className="roles-audit-item">
                <div className="roles-audit-icon">
                  <span className="material-symbols-outlined">{entry.icon}</span>
                </div>
                <div className="roles-audit-copy">
                  <strong>{entry.title}</strong>
                  <p>{entry.description}</p>
                </div>
                <span className="roles-audit-time">{entry.time}</span>
              </div>
            ))}
            {initialAuditHighlights.length === 0 ? (
              <div className="roles-audit-item">
                <div className="roles-audit-icon">
                  <span className="material-symbols-outlined">history</span>
                </div>
                <div className="roles-audit-copy">
                  <strong>{labels.noRoleChanges}</strong>
                  <p>{labels.noRoleChangesCopy}</p>
                </div>
              </div>
            ) : null}
          </div>
        </article>

        <aside className="roles-help-card">
          <div className="roles-help-copy">
            <p className="card-kicker">{labels.securityGuidance}</p>
            <h3>{labels.needHelp}</h3>
            <p>{labels.helpCopy}</p>
            <a href="/dashboard/settings/security" className="roles-help-button">
              {labels.documentation}
            </a>
          </div>
          <span className="material-symbols-outlined roles-help-mark">lock_person</span>
        </aside>
      </section>
    </>
  );
}
