<script>
  import { t } from './i18n'

  export let syncStatus = null
  export let syncLoading = false
  export let syncMessage = ''
  export let syncMessageKind = ''
  export let onSync = null
  export let onOpenSettings = null

  function getDotClass(status) {
    if (!status || !status.configured) return 'dot-disabled'
    if (syncLoading) return 'dot-syncing'
    if (status.revoked) return 'dot-disabled'
    if (status.connected) return 'dot-connected'
    if (status.configured) return 'dot-warning'
    return 'dot-disabled'
  }

  function getLabel(status) {
    if (!status || !status.configured) return t('sync.status.disabled')
    if (syncLoading) return t('sync.status.syncing')
    if (status.revoked) return t('sync.status.error')
    if (status.connected) return t('sync.status.connected')
    return t('sync.status.disconnected')
  }
</script>

<div class="sync-status-widget">
  {#if syncStatus?.configured}
    <div class="sync-status-row">
      <span class="sync-dot {getDotClass(syncStatus)}"></span>
      <span class="sync-label">{getLabel(syncStatus)}</span>
      {#if syncStatus.unpushedOps > 0}
        <span class="sync-count">{syncStatus.unpushedOps} {t('sync.changes')}</span>
      {/if}
      <div class="sync-actions-row">
        <button class="btn btn-xs" on:click={onSync} disabled={syncLoading}>
          {syncLoading ? t('sync.running') : t('sync.run')}
        </button>
        <button class="btn btn-xs btn-link" on:click={onOpenSettings}>
          {t('sync.details')}
        </button>
      </div>
    </div>
    {#if syncMessage}
      <div class={syncMessageKind === 'warning' ? 'sync-message sync-message-warning' : 'sync-message'}>
        {syncMessage}
      </div>
    {/if}
  {:else}
    <div class="sync-status-row">
      <span class="sync-dot dot-disabled"></span>
      <span class="sync-label">{t('sync.status.disabled')}</span>
      <button class="btn btn-xs" on:click={onOpenSettings}>{t('sync.configure')}</button>
    </div>
  {/if}
</div>

<style>
  .sync-status-widget {
    font-size: 0.8rem;
    padding: 0.4rem 0.5rem;
  }
  .sync-status-row {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    flex-wrap: wrap;
  }
  .sync-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
  }
  .dot-connected { background: #34d399; box-shadow: 0 0 4px rgba(52, 211, 153, 0.4); }
  .dot-warning { background: #f59e0b; }
  .dot-disabled { background: #555; }
  .dot-syncing { background: #818cf8; animation: pulse 1s infinite; }
  @keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
  }
  .sync-label {
    color: var(--text-dim, #888);
    white-space: nowrap;
  }
  .sync-count {
    color: var(--accent, #818cf8);
    font-weight: 600;
  }
  .sync-actions-row {
    display: flex;
    gap: 0.3rem;
    margin-left: auto;
  }
  .sync-message {
    margin-top: 0.25rem;
    color: var(--text-dim, #888);
    line-height: 1.25;
  }
  .sync-message-warning {
    color: #f59e0b;
  }
  .btn-xs {
    padding: 0.2rem 0.5rem;
    font-size: 0.75rem;
    border: 1px solid var(--border, #2a2a3e);
    background: var(--surface-alt, #252538);
    color: var(--text-dim, #888);
    border-radius: 4px;
    cursor: pointer;
    white-space: nowrap;
  }
  .btn-xs:hover {
    background: var(--border, #2a2a3e);
    color: var(--text, #e0e0e0);
  }
  .btn-xs:disabled {
    opacity: 0.5;
    cursor: default;
  }
  .btn-link {
    border: none;
    background: transparent;
    color: var(--accent, #818cf8);
    text-decoration: underline;
  }
  .btn-link:hover {
    color: #a5b4fc;
    background: transparent;
  }
</style>
