import { css, cssManager, unsafeCSS } from '@design.estate/dees-element';

export const fonts = {
  base: `'Geist Sans', -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif`,
  mono: `'Geist Mono', ui-monospace, SFMono-Regular, "SF Mono", Consolas, "Liberation Mono", Menlo, monospace`
};

export const colors = {
  background: {
    primary: cssManager.bdTheme('#ffffff', '#09090b'),
    secondary: cssManager.bdTheme('#fafafa', '#18181b'),
    muted: cssManager.bdTheme('#f4f4f5', '#27272a'),
    card: cssManager.bdTheme('#ffffff', '#0f0f12'),
    elevated: cssManager.bdTheme('#ffffff', '#1a1a1e')
  },
  border: {
    default: cssManager.bdTheme('#e4e4e7', '#27272a'),
    muted: cssManager.bdTheme('#f4f4f5', '#3f3f46'),
    subtle: cssManager.bdTheme('#f0f0f2', '#1f1f23'),
    light: cssManager.bdTheme('#f4f4f5', '#27272a'),
    strong: cssManager.bdTheme('#d4d4d8', '#3f3f46')
  },
  text: {
    primary: cssManager.bdTheme('#09090b', '#fafafa'),
    secondary: cssManager.bdTheme('#71717a', '#a1a1aa'),
    muted: cssManager.bdTheme('#a1a1aa', '#71717a')
  },
  status: {
    operational: cssManager.bdTheme('#16a34a', '#22c55e'),
    degraded: cssManager.bdTheme('#d97706', '#fbbf24'),
    partial: cssManager.bdTheme('#dc2626', '#f87171'),
    major: cssManager.bdTheme('#b91c1c', '#ef4444'),
    maintenance: cssManager.bdTheme('#2563eb', '#60a5fa'),
    partialOutage: cssManager.bdTheme('#f97316', '#fb923c'),
    majorOutage: cssManager.bdTheme('#ef4444', '#f87171')
  },
  accent: {
    primary: cssManager.bdTheme('#3b82f6', '#60a5fa'),
    hover: cssManager.bdTheme('#2563eb', '#3b82f6'),
    focus: cssManager.bdTheme('#3b82f6', '#60a5fa'),
    danger: cssManager.bdTheme('#dc2626', '#ef4444'),
    dangerHover: cssManager.bdTheme('#b91c1c', '#dc2626'),
    success: cssManager.bdTheme('#16a34a', '#22c55e'),
    warning: cssManager.bdTheme('#f59e0b', '#fbbf24')
  }
};

export const shadows = {
  xs: '0 1px 2px 0 rgba(0, 0, 0, 0.03)',
  sm: '0 1px 3px 0 rgba(0, 0, 0, 0.06), 0 1px 2px -1px rgba(0, 0, 0, 0.06)',
  base: '0 4px 6px -1px rgba(0, 0, 0, 0.07), 0 2px 4px -2px rgba(0, 0, 0, 0.05)',
  md: '0 6px 12px -2px rgba(0, 0, 0, 0.08), 0 3px 7px -3px rgba(0, 0, 0, 0.05)',
  lg: '0 12px 24px -4px rgba(0, 0, 0, 0.1), 0 6px 12px -6px rgba(0, 0, 0, 0.05)',
  xl: '0 24px 48px -12px rgba(0, 0, 0, 0.12), 0 12px 24px -12px rgba(0, 0, 0, 0.05)',
  inner: 'inset 0 2px 4px 0 rgba(0, 0, 0, 0.04)'
};

export const borderRadius = {
  xs: '3px',
  sm: '4px',
  base: '6px',
  md: '8px',
  lg: '12px',
  xl: '16px',
  '2xl': '24px',
  full: '9999px'
};

export const spacing = {
  xs: '4px',
  sm: '8px',
  md: '16px',
  lg: '24px',
  xl: '32px',
  '2xl': '48px',
  '3xl': '64px',
  '4xl': '96px'
};

export const easings = {
  default: 'cubic-bezier(0.4, 0, 0.2, 1)',
  smooth: 'cubic-bezier(0.4, 0, 0.6, 1)',
  bounce: 'cubic-bezier(0.68, -0.55, 0.265, 1.55)',
  snappy: 'cubic-bezier(0.2, 0, 0, 1)',
  spring: 'cubic-bezier(0.175, 0.885, 0.32, 1.275)'
};

export const durations = {
  instant: '50ms',
  fast: '100ms',
  normal: '200ms',
  slow: '300ms',
  slower: '500ms',
  slowest: '800ms'
};

export const commonStyles = css`
  /* Button styles */
  .button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-family: ${unsafeCSS(fonts.base)};
    font-size: 13px;
    font-weight: 500;
    height: 36px;
    padding: 0 14px;
    border-radius: ${unsafeCSS(borderRadius.base)};
    border: 1px solid ${colors.border.default};
    background: ${colors.background.primary};
    color: ${colors.text.primary};
    cursor: pointer;
    user-select: none;
    transition: all ${unsafeCSS(durations.normal)} ${unsafeCSS(easings.default)};
    gap: 6px;
    white-space: nowrap;
    letter-spacing: -0.01em;
  }

  .button:hover {
    background: ${colors.background.secondary};
    border-color: ${cssManager.bdTheme('#d4d4d8', '#3f3f46')};
    box-shadow: ${unsafeCSS(shadows.xs)};
  }

  .button:active {
    transform: scale(0.98);
  }

  .button:focus-visible {
    outline: 2px solid ${colors.accent.focus};
    outline-offset: 2px;
  }

  .button.primary {
    background: ${colors.accent.primary};
    color: #ffffff;
    border-color: transparent;
  }

  .button.primary:hover {
    background: ${colors.accent.hover};
    box-shadow: ${unsafeCSS(shadows.sm)};
  }

  .button.danger {
    background: ${colors.accent.danger};
    color: #ffffff;
    border-color: transparent;
  }

  .button.danger:hover {
    background: ${colors.accent.dangerHover};
  }

  .button.ghost {
    background: transparent;
    border-color: transparent;
  }

  .button.ghost:hover {
    background: ${colors.background.muted};
  }

  .button.sm {
    height: 32px;
    padding: 0 12px;
    font-size: 12px;
  }

  .button.lg {
    height: 44px;
    padding: 0 20px;
    font-size: 15px;
  }

  .button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    pointer-events: none;
  }

  /* Card styles */
  .card {
    background: ${colors.background.card};
    border: 1px solid ${colors.border.default};
    border-radius: ${unsafeCSS(borderRadius.lg)};
    padding: ${unsafeCSS(spacing.lg)};
    box-shadow: ${unsafeCSS(shadows.sm)};
  }

  /* Form styles */
  .form-group {
    margin-bottom: ${unsafeCSS(spacing.md)};
  }

  .form-label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: ${colors.text.primary};
    margin-bottom: ${unsafeCSS(spacing.xs)};
  }

  .form-label.required::after {
    content: ' *';
    color: ${colors.accent.danger};
  }

  .form-input {
    display: block;
    width: 100%;
    height: 40px;
    padding: 0 12px;
    font-family: ${unsafeCSS(fonts.base)};
    font-size: 14px;
    color: ${colors.text.primary};
    background: ${colors.background.primary};
    border: 1px solid ${colors.border.default};
    border-radius: ${unsafeCSS(borderRadius.base)};
    transition: border-color ${unsafeCSS(durations.fast)} ${unsafeCSS(easings.default)},
                box-shadow ${unsafeCSS(durations.fast)} ${unsafeCSS(easings.default)};
    box-sizing: border-box;
  }

  .form-input:focus {
    outline: none;
    border-color: ${colors.accent.primary};
    box-shadow: 0 0 0 3px ${cssManager.bdTheme('rgba(59, 130, 246, 0.1)', 'rgba(96, 165, 250, 0.15)')};
  }

  .form-input::placeholder {
    color: ${colors.text.muted};
  }

  .form-input.error {
    border-color: ${colors.accent.danger};
  }

  .form-input.error:focus {
    box-shadow: 0 0 0 3px ${cssManager.bdTheme('rgba(220, 38, 38, 0.1)', 'rgba(248, 113, 113, 0.15)')};
  }

  .form-textarea {
    min-height: 100px;
    padding: 12px;
    resize: vertical;
    line-height: 1.5;
  }

  .form-select {
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%2371717a' d='M2.5 4.5L6 8l3.5-3.5'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 36px;
  }

  .form-error {
    font-size: 12px;
    color: ${colors.accent.danger};
    margin-top: ${unsafeCSS(spacing.xs)};
  }

  .form-hint {
    font-size: 12px;
    color: ${colors.text.muted};
    margin-top: ${unsafeCSS(spacing.xs)};
  }

  /* Checkbox and toggle */
  .form-checkbox {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    font-size: 14px;
    color: ${colors.text.primary};
  }

  .form-checkbox input[type="checkbox"] {
    width: 18px;
    height: 18px;
    border: 1px solid ${colors.border.default};
    border-radius: ${unsafeCSS(borderRadius.sm)};
    background: ${colors.background.primary};
    cursor: pointer;
    appearance: none;
    transition: all ${unsafeCSS(durations.fast)} ${unsafeCSS(easings.default)};
  }

  .form-checkbox input[type="checkbox"]:checked {
    background: ${colors.accent.primary};
    border-color: ${colors.accent.primary};
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='white' d='M10 3L4.5 8.5L2 6'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: center;
  }

  /* Table styles */
  .table-container {
    overflow-x: auto;
    border: 1px solid ${colors.border.default};
    border-radius: ${unsafeCSS(borderRadius.lg)};
    background: ${colors.background.card};
  }

  .table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
  }

  .table th {
    text-align: left;
    padding: 12px 16px;
    font-weight: 600;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: ${colors.text.muted};
    background: ${colors.background.secondary};
    border-bottom: 1px solid ${colors.border.default};
  }

  .table th.sortable {
    cursor: pointer;
    user-select: none;
  }

  .table th.sortable:hover {
    color: ${colors.text.primary};
  }

  .table td {
    padding: 12px 16px;
    border-bottom: 1px solid ${colors.border.subtle};
    color: ${colors.text.primary};
  }

  .table tr:last-child td {
    border-bottom: none;
  }

  .table tr:hover td {
    background: ${colors.background.secondary};
  }

  .table .actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
  }

  /* Status badge */
  .status-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 10px;
    border-radius: ${unsafeCSS(borderRadius.full)};
    font-size: 12px;
    font-weight: 500;
  }

  .status-badge .dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
  }

  .status-badge.operational {
    background: ${cssManager.bdTheme('rgba(22, 163, 74, 0.1)', 'rgba(34, 197, 94, 0.15)')};
    color: ${cssManager.bdTheme('#15803d', '#4ade80')};
  }

  .status-badge.operational .dot {
    background: ${colors.status.operational};
  }

  .status-badge.degraded {
    background: ${cssManager.bdTheme('rgba(217, 119, 6, 0.1)', 'rgba(251, 191, 36, 0.15)')};
    color: ${cssManager.bdTheme('#b45309', '#fcd34d')};
  }

  .status-badge.degraded .dot {
    background: ${colors.status.degraded};
  }

  .status-badge.partial_outage,
  .status-badge.major_outage {
    background: ${cssManager.bdTheme('rgba(220, 38, 38, 0.1)', 'rgba(248, 113, 113, 0.15)')};
    color: ${cssManager.bdTheme('#b91c1c', '#fca5a5')};
  }

  .status-badge.partial_outage .dot,
  .status-badge.major_outage .dot {
    background: ${colors.status.major};
  }

  .status-badge.maintenance {
    background: ${cssManager.bdTheme('rgba(37, 99, 235, 0.1)', 'rgba(96, 165, 250, 0.15)')};
    color: ${cssManager.bdTheme('#1d4ed8', '#93c5fd')};
  }

  .status-badge.maintenance .dot {
    background: ${colors.status.maintenance};
  }

  /* Severity badge */
  .severity-badge {
    display: inline-flex;
    padding: 2px 8px;
    border-radius: ${unsafeCSS(borderRadius.sm)};
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.02em;
  }

  .severity-badge.critical {
    background: ${cssManager.bdTheme('#fef2f2', '#450a0a')};
    color: ${cssManager.bdTheme('#b91c1c', '#fca5a5')};
  }

  .severity-badge.major {
    background: ${cssManager.bdTheme('#fff7ed', '#431407')};
    color: ${cssManager.bdTheme('#c2410c', '#fdba74')};
  }

  .severity-badge.minor {
    background: ${cssManager.bdTheme('#fefce8', '#422006')};
    color: ${cssManager.bdTheme('#a16207', '#fde047')};
  }

  .severity-badge.maintenance {
    background: ${cssManager.bdTheme('#eff6ff', '#172554')};
    color: ${cssManager.bdTheme('#1d4ed8', '#93c5fd')};
  }

  /* Tabs */
  .tabs {
    display: flex;
    gap: 4px;
    border-bottom: 1px solid ${colors.border.default};
    margin-bottom: ${unsafeCSS(spacing.lg)};
  }

  .tab {
    padding: 12px 16px;
    font-size: 14px;
    font-weight: 500;
    color: ${colors.text.secondary};
    background: transparent;
    border: none;
    cursor: pointer;
    position: relative;
    transition: color ${unsafeCSS(durations.fast)} ${unsafeCSS(easings.default)};
  }

  .tab:hover {
    color: ${colors.text.primary};
  }

  .tab.active {
    color: ${colors.accent.primary};
  }

  .tab.active::after {
    content: '';
    position: absolute;
    bottom: -1px;
    left: 0;
    right: 0;
    height: 2px;
    background: ${colors.accent.primary};
    border-radius: 1px;
  }

  /* Empty state */
  .empty-state {
    text-align: center;
    padding: ${unsafeCSS(spacing['2xl'])} ${unsafeCSS(spacing.lg)};
    color: ${colors.text.muted};
  }

  .empty-state-icon {
    font-size: 48px;
    margin-bottom: ${unsafeCSS(spacing.md)};
    opacity: 0.5;
  }

  .empty-state-title {
    font-size: 16px;
    font-weight: 600;
    color: ${colors.text.primary};
    margin-bottom: ${unsafeCSS(spacing.xs)};
  }

  .empty-state-description {
    font-size: 14px;
    margin-bottom: ${unsafeCSS(spacing.lg)};
  }

  /* Modal/Dialog */
  .modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: ${unsafeCSS(spacing.lg)};
  }

  .modal {
    background: ${colors.background.card};
    border-radius: ${unsafeCSS(borderRadius.xl)};
    box-shadow: ${unsafeCSS(shadows.xl)};
    max-width: 500px;
    width: 100%;
    max-height: 90vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  .modal-header {
    padding: ${unsafeCSS(spacing.lg)};
    border-bottom: 1px solid ${colors.border.default};
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .modal-title {
    font-size: 18px;
    font-weight: 600;
    color: ${colors.text.primary};
  }

  .modal-close {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: transparent;
    color: ${colors.text.muted};
    cursor: pointer;
    border-radius: ${unsafeCSS(borderRadius.base)};
    transition: all ${unsafeCSS(durations.fast)} ${unsafeCSS(easings.default)};
  }

  .modal-close:hover {
    background: ${colors.background.muted};
    color: ${colors.text.primary};
  }

  .modal-body {
    padding: ${unsafeCSS(spacing.lg)};
    overflow-y: auto;
    flex: 1;
  }

  .modal-footer {
    padding: ${unsafeCSS(spacing.lg)};
    border-top: 1px solid ${colors.border.default};
    display: flex;
    gap: 12px;
    justify-content: flex-end;
  }

  /* Search input */
  .search-input {
    position: relative;
  }

  .search-input input {
    padding-left: 40px;
  }

  .search-input .search-icon {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: ${colors.text.muted};
    pointer-events: none;
  }

  /* Animations */
  @keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
  }

  @keyframes slideUp {
    from {
      opacity: 0;
      transform: translateY(16px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  .fade-in {
    animation: fadeIn ${unsafeCSS(durations.normal)} ${unsafeCSS(easings.default)};
  }

  .slide-up {
    animation: slideUp ${unsafeCSS(durations.slow)} ${unsafeCSS(easings.default)};
  }
`;

export const getStatusColor = (status: string) => {
  switch (status) {
    case 'operational':
      return colors.status.operational;
    case 'degraded':
      return colors.status.degraded;
    case 'partial_outage':
      return colors.status.partial;
    case 'major_outage':
      return colors.status.major;
    case 'maintenance':
      return colors.status.maintenance;
    default:
      return colors.text.secondary;
  }
};

export const getSeverityLabel = (severity: string) => {
  switch (severity) {
    case 'critical':
      return 'Critical';
    case 'major':
      return 'Major';
    case 'minor':
      return 'Minor';
    case 'maintenance':
      return 'Maintenance';
    default:
      return severity;
  }
};

export const getStatusLabel = (status: string) => {
  switch (status) {
    case 'operational':
      return 'Operational';
    case 'degraded':
      return 'Degraded';
    case 'partial_outage':
      return 'Partial Outage';
    case 'major_outage':
      return 'Major Outage';
    case 'maintenance':
      return 'Maintenance';
    case 'investigating':
      return 'Investigating';
    case 'identified':
      return 'Identified';
    case 'monitoring':
      return 'Monitoring';
    case 'resolved':
      return 'Resolved';
    case 'postmortem':
      return 'Postmortem';
    default:
      return status;
  }
};
