<template>
  <AppLayout title="Create Result">
    <div class="space-y-6">
      <!-- Header -->
      <div class="flex items-center justify-between">
        <div>
          <h1 class="text-3xl font-bold tracking-tight">Create Result</h1>
          <p class="text-muted-foreground">
            Add a new student examination result
          </p>
        </div>
      </div>

      <!-- Form Card -->
      <Card>
        <CardHeader>
          <CardTitle>Student Results Entry</CardTitle>
          <CardDescription>
            Enter examination results for multiple subjects for a student
          </CardDescription>
        </CardHeader>
        <CardContent>
          <form @submit.prevent="submit" class="space-y-6">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
              <!-- Academic Year -->
              <div class="space-y-2">
                <Label for="academic_year_id">Academic Year *</Label>
                <Select v-model="form.academic_year_id" @update:model-value="onAcademicYearChange">
                  <SelectTrigger :class="{ 'border-destructive': form.errors.academic_year_id }">
                    <SelectValue placeholder="Select academic year" />
                  </SelectTrigger>
                  <SelectContent>
                    <SelectItem
                      v-for="academicYear in academicYears"
                      :key="academicYear.id"
                      :value="academicYear.id.toString()"
                    >
                      {{ academicYear.name }}
                    </SelectItem>
                  </SelectContent>
                </Select>
                <p v-if="form.errors.academic_year_id" class="text-sm text-destructive">
                  {{ form.errors.academic_year_id }}
                </p>
              </div>

              <!-- Exam Type -->
              <div class="space-y-2">
                <Label for="exam_type_id">Exam Type *</Label>
                <Select v-model="form.exam_type_id">
                  <SelectTrigger :class="{ 'border-destructive': form.errors.exam_type_id }">
                    <SelectValue placeholder="Select exam type" />
                  </SelectTrigger>
                  <SelectContent>
                    <SelectItem
                      v-for="examType in filteredExamTypes"
                      :key="examType.id"
                      :value="examType.id.toString()"
                    >
                      {{ examType.name }}
                    </SelectItem>
                  </SelectContent>
                </Select>
                <p v-if="form.errors.exam_type_id" class="text-sm text-destructive">
                  {{ form.errors.exam_type_id }}
                </p>
              </div>

              <!-- Classroom -->
              <div class="space-y-2">
                <Label for="classroom_id">Classroom *</Label>
                <Select v-model="form.classroom_id" @update:model-value="onClassroomChange">
                  <SelectTrigger :class="{ 'border-destructive': form.errors.classroom_id }">
                    <SelectValue placeholder="Select classroom" />
                  </SelectTrigger>
                  <SelectContent>
                    <SelectItem
                      v-for="classroom in filteredClassrooms"
                      :key="classroom.id"
                      :value="classroom.id.toString()"
                    >
                      {{ classroom.name }} ({{ classroom.academic_year?.name }})
                    </SelectItem>
                  </SelectContent>
                </Select>
                <p v-if="form.errors.classroom_id" class="text-sm text-destructive">
                  {{ form.errors.classroom_id }}
                </p>
              </div>

              <!-- Student -->
              <div class="space-y-2">
                <Label for="student_id">Student *</Label>
                <Select v-model="form.student_id">
                  <SelectTrigger :class="{ 'border-destructive': form.errors.student_id }">
                    <SelectValue placeholder="Select student" />
                  </SelectTrigger>
                  <SelectContent>
                    <SelectItem
                      v-for="student in students"
                      :key="student.id"
                      :value="student.id.toString()"
                    >
                      {{ student.user?.first_name }} {{ student.user?.last_name }} ({{ student.student_id }})
                    </SelectItem>
                  </SelectContent>
                </Select>
                <p v-if="form.errors.student_id" class="text-sm text-destructive">
                  {{ form.errors.student_id }}
                </p>
              </div>

              <!-- Subject -->
              <div class="space-y-2">
                <Label for="subject_id">Subject *</Label>
                <Select v-model="form.subject_id">
                  <SelectTrigger :class="{ 'border-destructive': form.errors.subject_id }">
                    <SelectValue placeholder="Select subject" />
                  </SelectTrigger>
                  <SelectContent>
                    <SelectItem
                      v-for="subject in subjects"
                      :key="subject.id"
                      :value="subject.id.toString()"
                    >
                      {{ subject.name }} ({{ subject.code }})
                    </SelectItem>
                  </SelectContent>
                </Select>
                <p v-if="form.errors.subject_id" class="text-sm text-destructive">
                  {{ form.errors.subject_id }}
                </p>
              </div>

              <!-- Marks -->
              <div class="space-y-2">
                <Label for="marks">Marks *</Label>
                <Input
                  id="marks"
                  v-model="form.marks"
                  type="number"
                  min="0"
                  max="100"
                  placeholder="e.g., 85"
                  :class="{ 'border-destructive': form.errors.marks }"
                />
                <p v-if="form.errors.marks" class="text-sm text-destructive">
                  {{ form.errors.marks }}
                </p>
                <p class="text-sm text-muted-foreground">
                  Enter marks between 0 and 100
                </p>
              </div>

              <!-- Grade (Optional) -->
              <div class="space-y-2">
                <Label for="grade">Grade (Optional)</Label>
                <Input
                  id="grade"
                  v-model="form.grade"
                  type="text"
                  placeholder="e.g., A, B+, C"
                  maxlength="5"
                  :class="{ 'border-destructive': form.errors.grade }"
                />
                <p v-if="form.errors.grade" class="text-sm text-destructive">
                  {{ form.errors.grade }}
                </p>
                <p class="text-sm text-muted-foreground">
                  Grade will be auto-calculated if left empty
                </p>
              </div>

              <!-- Points (Optional) -->
              <div class="space-y-2">
                <Label for="points">Points (Optional)</Label>
                <Input
                  id="points"
                  v-model="form.points"
                  type="number"
                  min="0"
                  max="12"
                  placeholder="e.g., 8"
                  :class="{ 'border-destructive': form.errors.points }"
                />
                <p v-if="form.errors.points" class="text-sm text-destructive">
                  {{ form.errors.points }}
                </p>
                <p class="text-sm text-muted-foreground">
                  Points will be auto-calculated if left empty
                </p>
              </div>
            </div>

            <!-- Form Actions -->
            <div class="flex items-center justify-end space-x-3 pt-6 border-t">
              <Button variant="outline" type="button" as-child>
                <Link :href="route('results.index')">
                  Cancel
                </Link>
              </Button>
              <Button type="submit" :disabled="form.processing">
                <span v-if="form.processing">Creating...</span>
                <span v-else>Create Result</span>
              </Button>
            </div>
          </form>
        </CardContent>
      </Card>
    </div>
  </AppLayout>
</template>

<script setup lang="ts">
import { useForm, Link } from '@inertiajs/vue3'
import { computed } from 'vue'
import AppLayout from '@/layouts/AppLayout.vue'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'

const props = defineProps({
  academicYears: {
    type: Array,
    default: () => []
  },
  examTypes: {
    type: Array,
    default: () => []
  },
  classrooms: {
    type: Array,
    default: () => []
  },
  subjects: {
    type: Array,
    default: () => []
  },
  students: {
    type: Array,
    default: () => []
  }
})

const form = useForm({
  student_id: '',
  subject_id: '',
  classroom_id: '',
  academic_year_id: '',
  exam_type_id: '',
  marks: '',
  grade: '',
  points: ''
})

const filteredExamTypes = computed(() => {
  if (!form.academic_year_id) return []
  return props.examTypes.filter(examType =>
    examType.academic_year_id.toString() === form.academic_year_id
  )
})

const filteredClassrooms = computed(() => {
  if (!form.academic_year_id) return []
  return props.classrooms.filter(classroom =>
    classroom.academic_year_id.toString() === form.academic_year_id
  )
})

const onAcademicYearChange = () => {
  // Reset dependent fields when academic year changes
  form.exam_type_id = ''
  form.classroom_id = ''
}

const onClassroomChange = () => {
  // Could add logic to filter students by classroom if needed
}

const submit = () => {
  form.post(route('results.store'))
}
</script>
