<script setup>
import { ref, computed } from 'vue';

// Props definition using defineProps
const props = defineProps({
  title: {
    type: String,
    required: true
  },
  company: {
    type: String,
    required: true
  },
  location: {
    type: String,
    default: 'Remote'
  },
  salary: {
    type: String,
    default: 'Not specified'
  },
  description: {
    type: String,
    required: true
  },
  applied: {
    type: Boolean,
    default: false
  },
  tags: {
    type: Array,
    default: () => []
  }
});

// Reactive state using ref
const isApplied = ref(props.applied);

const applyForJob = () => {
  isApplied.value = true;
  // Emit an event that parent components can listen to
  emit('apply', props.title);
};

// Generate a random pastel color for company logo background
const generateRandomColor = () => {
  // Generate pastel colors by using higher base values
  const r = Math.floor(Math.random() * 100) + 155;
  const g = Math.floor(Math.random() * 100) + 155;
  const b = Math.floor(Math.random() * 100) + 155;
  return `rgb(${r}, ${g}, ${b})`;
};

// Define emits
const emit = defineEmits(['apply', 'click']);
</script>

<template>
  <div class="job-card" @click="emit('click')">
    <div class="job-content">
      <div class="job-main-info">
        <div class="company-logo" :style="{ backgroundColor: generateRandomColor() }">
          {{ company.charAt(0) }}
        </div>
        <div class="job-header">
          <h3 class="job-title">{{ title }}</h3>
          <div class="job-company">{{ company }}</div>
        </div>
      </div>
      
      <div class="job-tags" v-if="tags && tags.length > 0">
        <span v-for="(tag, index) in tags" :key="index" class="job-tag">
          {{ tag }}
        </span>
      </div>
      
      <div class="job-details-row">
        <div class="job-meta">
          <div class="meta-item">
            <span class="meta-icon">📍</span>
            <span class="meta-text">{{ location }}</span>
          </div>
          <div class="meta-item">
            <span class="meta-icon">💰</span>
            <span class="meta-text">{{ salary }}</span>
          </div>
        </div>
        
        <div class="job-actions">
          <button 
            class="action-button apply-button" 
            :class="{ 'applied': isApplied }"
            @click.stop="applyForJob"
            :disabled="isApplied"
          >
            {{ isApplied ? 'Applied' : 'Apply Now' }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.job-card {
  background-color: var(--card-bg, #fff);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.08);
  border-radius: 12px;
  border: 1px solid var(--card-border, #eee);
  overflow: hidden;
  transition: all 0.3s ease;
  position: relative;
  cursor: pointer;
  padding: 1.5rem;
  
  &:hover {
    box-shadow: 0 12px 28px rgba(0, 0, 0, 0.12);
    transform: translateY(-3px);
  }
  
  .job-content {
    padding: 0;
  }
  
  .job-main-info {
    display: flex;
    align-items: center;
    margin-bottom: 1.5rem;
    
    .company-logo {
      width: 70px;
      height: 70px;
      border-radius: 14px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 1.8rem;
      font-weight: 700;
      color: white;
      margin-right: 1.25rem;
      flex-shrink: 0;
    }
  }
  
  .job-header {
    flex: 1;
    
    .job-title {
      margin: 0 0 0.75rem;
      color: var(--text-color, #333);
      font-size: 1.5rem;
      font-weight: 600;
    }
    
    .job-company {
      font-weight: 500;
      color: var(--text-color-secondary, #555);
      font-size: 1.1rem;
    }
  }
  
  .job-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin: 0.75rem 0;
    
    .job-tag {
      font-size: 0.8rem;
      padding: 0.25rem 0.6rem;
      border-radius: 16px;
      background-color: var(--tag-bg, rgba(52, 152, 219, 0.1));
      color: var(--primary-color, #3498db);
      font-weight: 500;
      transition: all 0.2s ease;
      
      &:hover {
        background-color: var(--tag-bg-hover, rgba(52, 152, 219, 0.2));
      }
      
      @media (prefers-color-scheme: dark) {
        background-color: rgba(52, 152, 219, 0.2);
        
        &:hover {
          background-color: rgba(52, 152, 219, 0.3);
        }
      }
    }
  }
  
  .job-details-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 1.5rem;
    flex-wrap: wrap;
    gap: 1.25rem;
    
    @media (max-width: 768px) {
      flex-direction: column;
      align-items: flex-start;
    }
  }
  
  .job-meta {
    display: flex;
    gap: 1.5rem;
    
    .meta-item {
      display: flex;
      align-items: center;
      font-size: 0.95rem;
      color: var(--text-color-secondary, #777);
      
      .meta-icon {
        margin-right: 0.5rem;
        font-size: 1.1rem;
      }
    }
  }
  
  .job-actions {
    display: flex;
    gap: 1rem;
    
    .action-button {
      padding: 0.75rem 1.25rem;
      border-radius: 8px;
      font-weight: 500;
      cursor: pointer;
      transition: all 0.2s ease;
      border: none;
      font-size: 0.95rem;
      
      &.toggle-button {
        background-color: transparent;
        border: 1px solid var(--primary-color, #3498db);
        color: var(--primary-color, #3498db);
        
        &:hover {
          background-color: rgba(52, 152, 219, 0.1);
        }
      }
      
      &.apply-button {
        background-color: var(--primary-color, #3498db);
        color: white;
        
        &:hover:not(:disabled) {
          background-color: var(--primary-hover-color, #2980b9);
          transform: translateY(-2px);
          box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        
        &:disabled {
          opacity: 0.7;
          cursor: not-allowed;
        }
        
        &.applied {
          background-color: #27ae60;
        }
      }
    }
  }
  
  .job-description-container {
    padding-top: 1.25rem;
    border-top: 1px solid var(--card-border, #eee);
    
    .job-description {
      margin: 0;
      line-height: 1.6;
      color: var(--text-color, #444);
    }
  }
}
</style>
