<script setup lang="ts">
import Loading from '@/components/Loading.vue';
import { inject, onMounted, ref } from 'vue';
import { api } from '../services/api';

const d = ref<any>(null);
const openGraph = inject<(key: string) => void>('openGraph');

onMounted(async () => d.value = await api.current());
</script>

<style scoped lang="scss">
.precip-card {
	position: relative;
	border-radius: 12px;
	background: var(--surface);
	border: 1px solid var(--border);
	padding: 14px;
	display: flex;
	flex-direction: column;
	gap: 12px;
}
.card-title {
	font-size: 13px;
	font-weight: 700;
	color: var(--text);
}
.divider {
	height: 1px;
	background: var(--border);
}
.stat-grid {
	display: grid;
	grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
	gap: 10px;
}
.stat {
	display: flex;
	flex-direction: column;
	gap: 2px;
	cursor: pointer;
	border-radius: 8px;
	padding: 4px 6px;
	margin: -4px -6px;
	transition: background 0.15s;
	&:hover { background: var(--hover); }
}
.stat-label {
	font-size: 10px;
	text-transform: uppercase;
	letter-spacing: 0.06em;
	color: var(--text-muted);
}
.stat-value {
	font-size: 14px;
	font-weight: 600;
	color: var(--text);
}
</style>

<template>
	<div class="precip-card" v-if="d">
		<div class="card-title">🌧️ Precipitation</div>

		<div class="stat-grid">
			<div class="stat">
				<span class="stat-label">Raining</span>
				<div v-if="!d" class="w-100 pos-rel br-2 overflow-hidden" style="height: 1.75em"><Loading /></div>
				<span v-else class="stat-value">{{ d.raining == 'True' ? d.storm_direction : 'No' }}</span>
			</div>
			<div class="stat" @click="openGraph?.('lightning_rate')">
				<span class="stat-label">Lightning</span>
				<div v-if="!d" class="w-100 pos-rel br-2 overflow-hidden" style="height: 1.75em"><Loading /></div>
				<span v-else class="stat-value">{{ d.lightning_rate }}<span class="fg-muted">/hr</span></span>
			</div>
			<div class="stat" @click="openGraph?.('lightning_distance')">
				<span class="stat-label">Distance</span>
				<div v-if="!d" class="w-100 pos-rel br-2 overflow-hidden" style="height: 1.75em"><Loading /></div>
				<span v-else class="stat-value">{{ d.lightning_distance }} <span class="fg-muted">km</span></span>
			</div>
		</div>

		<div class="divider" />

		<div class="stat-grid">
			<div class="stat" @click="openGraph?.('precipitation_chance')">
				<span class="stat-label">Chance</span>
				<div v-if="!d" class="w-100 pos-rel br-2 overflow-hidden" style="height: 1.75em"><Loading /></div>
				<span v-else class="stat-value">{{ d.precipitation_chance }}<span class="fg-muted">%</span></span>
			</div>
			<div class="stat" @click="openGraph?.('pressure_hpa')">
				<span class="stat-label">Pressure</span>
				<div v-if="!d" class="w-100 pos-rel br-2 overflow-hidden" style="height: 1.75em"><Loading /></div>
				<span v-else class="stat-value">{{ d.pressure_hpa.toFixed(1) }} <span class="fg-muted">({{d.pressure_trend}})</span></span>
			</div>
			<div class="stat" @click="openGraph?.('precipitation')">
				<span class="stat-label">Precipitation</span>
				<div v-if="!d" class="w-100 pos-rel br-2 overflow-hidden" style="height: 1.75em"><Loading /></div>
				<span v-else class="stat-value">{{ d.precipitation.toFixed(1) }} <span class="fg-muted">mm</span></span>
			</div>
			<div class="stat" @click="openGraph?.('accumulation')">
				<span class="stat-label">Accumulation</span>
				<div v-if="!d" class="w-100 pos-rel br-2 overflow-hidden" style="height: 1.75em"><Loading /></div>
				<span v-else class="stat-value">{{ d.accumulation.toFixed(1) }} <span class="fg-muted">mm {{ d.accumulation_type != 'none' ? `(${d.accumulation_type})` : '' }}</span></span>
			</div>
			<div class="stat">
				<span class="stat-label">Dew Point</span>
				<div v-if="!d" class="w-100 pos-rel br-2 overflow-hidden" style="height: 1.75em"><Loading /></div>
				<span v-else class="stat-value">{{ d.dew_point.toFixed(1) }}<span class="fg-muted">°C</span></span>
			</div>
			<div class="stat">
				<span class="stat-label">Frost Risk</span>
				<div v-if="!d" class="w-100 pos-rel br-2 overflow-hidden" style="height: 1.75em"><Loading /></div>
				<span v-else class="stat-value">{{ d.frost_risk }}</span>
			</div>
		</div>
	</div>
</template>
