<!-- src/pages/customer/profile/profile/ProfileSummary.vue -->
<template>
  <div class="card-glass h-fit">
    <div class="card-body p-4 sm:p-5">
      <!-- Action Buttons - Two Row Layout -->
      <div class="grid grid-cols-3 gap-2">
        <!-- Row 1 -->
        <router-link :to="{ name: 'deliveryCreate', params: { id: customer.id } }"
                     class="btn btn-primary btn-sm h-auto py-2 flex flex-col items-center justify-center gap-1 hover:scale-105 transition-transform">
          <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"/>
          </svg>
          <span class="text-[10px] uppercase font-bold">Delivery</span>
        </router-link>

        <router-link :to="{ name: 'CalenderCustomer', params: { id: customer.id } }"
                     class="btn btn-info btn-sm h-auto py-2 flex flex-col items-center justify-center gap-1 hover:scale-105 transition-transform">
          <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"/>
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/>
          </svg>
          <span class="text-[10px] uppercase font-bold">Service</span>
        </router-link>

        <router-link :to="{ name: 'customerEdit', params: { id: customer.id } }"
                     class="btn btn-secondary btn-sm h-auto py-2 flex flex-col items-center justify-center gap-1 hover:scale-105 transition-transform">
          <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
          </svg>
          <span class="text-[10px] uppercase font-bold">Edit</span>
        </router-link>

        <!-- Row 2 -->
        <router-link :to="{ name: 'servicePlanEdit', params: { id: customer.id } }"
                     class="btn btn-accent btn-sm h-auto py-2 flex flex-col items-center justify-center gap-1 hover:scale-105 transition-transform">
          <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
          </svg>
          <span class="text-[10px] uppercase font-bold">Contract</span>
        </router-link>

        <button @click="$emit('toggleAutomatic', customer.id)"
                class="btn btn-sm h-auto py-2 flex flex-col items-center justify-center gap-1 col-span-2 hover:scale-105 transition-transform"
                :class="automatic_status === 1 ? 'btn-success' : 'btn-warning'">
          <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"/>
          </svg>
          <span class="text-[10px] uppercase font-bold">{{ automatic_status === 1 ? 'Set to Will Call' : 'Set to Auto' }}</span>
        </button>

        <!-- Alert button (full-width row 3) -->
        <router-link :to="{ name: 'customerAlert', params: { id: customer.id } }"
                     class="btn btn-error btn-sm h-auto py-2 flex flex-col items-center justify-center gap-1 col-span-3 hover:scale-105 transition-transform">
          <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L3.732 16.5c-.77.833.192 2.5 1.732 2.5z"/>
          </svg>
          <span class="text-[10px] uppercase font-bold">{{ hasAlert ? 'Alert Active' : 'Alert' }}</span>
        </router-link>
      </div>

      <div class="divider my-3"></div>

      <!-- Customer Details -->
      <div>
        <div class="flex justify-between items-center mb-1">
          <h2 class="text-xl font-bold text-base-content">
            {{ customer.customer_first_name }} {{ customer.customer_last_name }}
          </h2>
          <span class="badge badge-sm" :class="automatic_status === 1 ? 'badge-success' : 'badge-ghost'">
            {{ automatic_status === 1 ? 'Automatic' : 'Will Call' }}
          </span>
        </div>
        <div class="text-error text-xs font-semibold mt-1" v-if="!customer.correct_address">
          Possible Incorrect Address!
        </div>
        <div class="mt-2 space-y-1 text-sm bg-base-100/30 p-3 rounded-lg">
          <div class="flex items-start gap-2">
            <div class="flex-1 font-medium opacity-80">
              <p>{{ customer.customer_address }}<span v-if="customer.customer_apt">, {{ customer.customer_apt }}</span></p>
              <p>{{ customer.customer_town }}, {{ stateName(customer.customer_state) }} {{ customer.customer_zip }}</p>
            </div>
            <a :href="googleMapsUrl" target="_blank" rel="noopener noreferrer" 
               class="btn btn-circle btn-xs btn-ghost hover:btn-primary transition-colors"
               title="Open in Google Maps">
              <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" class="w-4 h-4">
                <path stroke-linecap="round" stroke-linejoin="round" d="M15 10.5a3 3 0 11-6 0 3 3 0 016 0z" />
                <path stroke-linecap="round" stroke-linejoin="round" d="M19.5 10.5c0 7.142-7.5 11.25-7.5 11.25S4.5 17.642 4.5 10.5a7.5 7.5 0 1115 0z" />
              </svg>
            </a>
          </div>
          <div class="divider my-1 opacity-10"></div>
          <p class="flex items-center gap-2">
            <svg class="w-3 h-3 opacity-50" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z"/></svg>
            {{ customer.customer_phone_number }}
          </p>
          <p class="flex items-center gap-2">
            <svg class="w-3 h-3 opacity-50" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"/></svg>
            <span class="badge badge-outline badge-xs">{{ homeTypeName(customer.customer_home_type) }}</span>
          </p>
          <div v-if="customer_description">
            <div class="divider my-1 opacity-10"></div>
            <p class="text-xs italic opacity-70">{{ customer_description }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';

interface Customer {
  id: number;
  customer_first_name: string;
  customer_last_name: string;
  correct_address: boolean;
  customer_address: string;
  customer_apt: string | null;
  customer_town: string;
  customer_state: number;
  customer_zip: string;
  customer_phone_number: string;
  customer_home_type: number;
}

interface Props {
  customer: Customer;
  automatic_status: number;
  customer_description?: string;
  hasAlert?: boolean;
}

const props = defineProps<Props>();
defineEmits(['toggleAutomatic']);

const stateName = (id: number) => ['MA', 'RI', 'NH', 'ME', 'VT', 'CT', 'NY'][id] || 'N/A';
const homeTypeName = (id: number) => ['Residential', 'Apartment', 'Condo', 'Commercial', 'Business', 'Construction', 'Container'][id] || 'Unknown';

const googleMapsUrl = computed(() => {
  const address = `${props.customer.customer_address}${props.customer.customer_apt ? ', ' + props.customer.customer_apt : ''}, ${props.customer.customer_town}, ${stateName(props.customer.customer_state)} ${props.customer.customer_zip}`;
  return `https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(address)}`;
});
</script>

