<!-- src/components/Terminal.svelte -->
<script lang="ts">
  import { terminalLines } from '../stores/appStore';
  import { onMount, onDestroy } from 'svelte';
  
  let terminalRef: HTMLDivElement;
  let isResizing = false;
  let startY: number;
  let startHeight: number;
  
  function handleMouseDown(e: MouseEvent): void {
    isResizing = true;
    startY = e.clientY;
    startHeight = parseInt(getComputedStyle(terminalRef).height, 10);
    
    document.addEventListener('mousemove', handleMouseMove);
    document.addEventListener('mouseup', handleMouseUp);
    e.preventDefault();
  }
  
  function handleMouseMove(e: MouseEvent): void {
    if (!isResizing) return;
    
    const height = startHeight - (e.clientY - startY);
    const minHeight = 100;
    const maxHeight = window.innerHeight * 0.8;
    
    if (height >= minHeight && height <= maxHeight) {
      terminalRef.style.height = `${height}px`;
    }
  }
  
  function handleMouseUp(): void {
    isResizing = false;
    document.removeEventListener('mousemove', handleMouseMove);
    document.removeEventListener('mouseup', handleMouseUp);
  }
  
  function scrollToBottom(): void {
    if (terminalRef) {
      const content = terminalRef.querySelector('.terminal-content') as HTMLDivElement;
      if (content) {
        content.scrollTop = content.scrollHeight;
      }
    }
  }
  
  // Auto-scroll when new lines are added
  $: $terminalLines, setTimeout(scrollToBottom, 0);
  
  onDestroy(() => {
    document.removeEventListener('mousemove', handleMouseMove);
    document.removeEventListener('mouseup', handleMouseUp);
  });
</script>

<div class="terminal" bind:this={terminalRef}>
  <div class="resize-handle" on:mousedown={handleMouseDown}></div>
  <div class="terminal-header">Logs</div>
  <div class="terminal-content">
    {#each $terminalLines as line}
      <div class="terminal-line">{line}</div>
    {/each}
  </div>
</div>

<style>
  .terminal {
    background-color: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius-sm);
    margin: var(--spacing-lg);
    height: 200px;
    position: relative;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  @media (max-height: 500px) {
    .terminal {
      display: none;
    }
  }

  .resize-handle {
    position: absolute;
    top: 10;
    left: 0;
    right: 0;
    height: 10px;
    cursor: row-resize;
    z-index: 10;
    background-color: transparent;
  }

  .resize-handle:hover {
    background-color: var(--accent-primary);
    border-radius: 100px;
    opacity: 0.5;
  }

  .terminal-header {
    background-color: var(--bg-header);
    padding: var(--spacing-sm) var(--spacing-md);
    border-bottom: 1px solid var(--border-color);
    font-size: 0.875rem;
    color: var(--text-secondary);
    border-radius: var(--border-radius-sm) var(--border-radius-sm) 0 0;
    user-select: none;
  }

  .terminal-content {
    padding: var(--spacing-md);
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    font-size: 0.75rem;
    line-height: 1.4;
    color: var(--text-primary);
    flex: 1;
    overflow-y: auto;
    white-space: pre-wrap;
    user-select: text;
  }

  .terminal-line {
    margin-bottom: 2px;
  }

  .terminal-line:last-child {
    margin-bottom: 0;
  }
</style>
