<script lang="ts">
	import type { StudentHomework } from '$lib/features/homework/api/studentHomework';

	let {
		homework,
		isDone = false,
		onToggleDone,
		onclick
	}: {
		homework: StudentHomework;
		isDone?: boolean;
		onToggleDone?: (id: string) => void;
		onclick?: (id: string) => void;
	} = $props();

	function formatDueDate(dateStr: string): string {
		const date = new Date(dateStr + 'T00:00:00');
		return date.toLocaleDateString('fr-FR', {
			weekday: 'short',
			day: 'numeric',
			month: 'long'
		});
	}

	function handleToggle(e: Event) {
		e.stopPropagation();
		onToggleDone?.(homework.id);
	}

	function handleClick() {
		onclick?.(homework.id);
	}

	function handleKeydown(e: KeyboardEvent) {
		if (e.key === 'Enter' || e.key === ' ') {
			e.preventDefault();
			handleClick();
		}
	}
</script>

<div
	class="homework-card"
	class:done={isDone}
	style:border-left-color={homework.subjectColor ?? '#3b82f6'}
	role="button"
	tabindex="0"
	onclick={handleClick}
	onkeydown={handleKeydown}
>
	<div class="card-header">
		<span class="subject-name" style:color={homework.subjectColor ?? '#3b82f6'}>
			{homework.subjectName}
		</span>
		<button
			class="toggle-done"
			class:checked={isDone}
			onclick={handleToggle}
			aria-label={isDone ? `Marquer "${homework.title}" comme à faire` : `Marquer "${homework.title}" comme fait`}
		>
			{#if isDone}
				<svg width="18" height="18" viewBox="0 0 18 18" fill="none">
					<rect x="1" y="1" width="16" height="16" rx="3" fill="#22c55e" stroke="#22c55e" stroke-width="2"/>
					<path d="M5 9l3 3 5-6" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
				</svg>
			{:else}
				<svg width="18" height="18" viewBox="0 0 18 18" fill="none">
					<rect x="1" y="1" width="16" height="16" rx="3" stroke="#d1d5db" stroke-width="2"/>
				</svg>
			{/if}
		</button>
	</div>

	<h3 class="card-title">{homework.title}</h3>

	<div class="card-footer">
		<span class="due-date">Pour le {formatDueDate(homework.dueDate)}</span>
		<span class="status-badge" class:done={isDone}>
			{isDone ? 'Fait' : 'À faire'}
		</span>
		{#if homework.submissionStatus === 'submitted'}
			<span class="submission-badge submission-submitted">Rendu</span>
		{:else if homework.submissionStatus === 'late'}
			<span class="submission-badge submission-late">Rendu en retard</span>
		{:else if homework.submissionStatus === 'draft'}
			<span class="submission-badge submission-draft">Brouillon</span>
		{/if}
		{#if homework.hasAttachments}
			<span class="attachment-indicator" title="Pièce(s) jointe(s)">
				<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
					<path d="M21.44 11.05l-9.19 9.19a6 6 0 01-8.49-8.49l9.19-9.19a4 4 0 015.66 5.66l-9.2 9.19a2 2 0 01-2.83-2.83l8.49-8.48"/>
				</svg>
			</span>
		{/if}
	</div>
</div>

<style>
	.homework-card {
		padding: 0.75rem;
		background: #f9fafb;
		border-radius: 0.5rem;
		border-left: 3px solid #3b82f6;
		cursor: pointer;
		transition: background 0.15s, box-shadow 0.15s;
	}

	.homework-card:hover {
		background: #f3f4f6;
		box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
	}

	.homework-card:focus-visible {
		outline: 2px solid #3b82f6;
		outline-offset: 2px;
	}

	.homework-card.done {
		opacity: 0.6;
		border-left-color: #22c55e !important;
	}

	.card-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 0.25rem;
	}

	.subject-name {
		font-size: 0.75rem;
		font-weight: 600;
		text-transform: uppercase;
	}

	.toggle-done {
		background: none;
		border: none;
		padding: 0.125rem;
		cursor: pointer;
		display: flex;
		align-items: center;
	}

	.card-title {
		margin: 0 0 0.25rem;
		font-size: 0.9375rem;
		font-weight: 500;
		color: #1f2937;
	}

	.homework-card.done .card-title {
		text-decoration: line-through;
		color: #6b7280;
	}

		.card-footer {
			display: flex;
			align-items: center;
			gap: 0.5rem;
			flex-wrap: wrap;
		}

		.due-date {
			font-size: 0.8125rem;
			color: #6b7280;
		}

		.status-badge {
			font-size: 0.75rem;
			font-weight: 600;
			color: #92400e;
			background: #fef3c7;
			border-radius: 999px;
			padding: 0.125rem 0.5rem;
		}

		.status-badge.done {
			color: #166534;
			background: #dcfce7;
		}

		.attachment-indicator {
			color: #6b7280;
			display: flex;
			align-items: center;
	}

	.submission-badge {
		font-size: 0.75rem;
		font-weight: 600;
		border-radius: 999px;
		padding: 0.125rem 0.5rem;
	}

	.submission-submitted {
		color: #166534;
		background: #dcfce7;
	}

	.submission-late {
		color: #991b1b;
		background: #fee2e2;
	}

	.submission-draft {
		color: #3730a3;
		background: #e0e7ff;
	}
</style>
