<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
    <t t-name="encoach_course_gen.GapAnalysis">
        <Layout display="{ controlPanel: {} }">
            <div class="o_gap_analysis">
                <div class="container-fluid py-3">

                    <t t-if="state.loading">
                        <div class="text-center py-5">
                            <i class="fa fa-spinner fa-spin fa-3x text-primary"/>
                            <p class="mt-2 text-muted">Analyzing gaps...</p>
                        </div>
                    </t>

                    <!-- Student Selector -->
                    <t t-elif="!state.studentId and state.studentList">
                        <h2 class="mb-3">Gap Analysis</h2>
                        <p class="text-muted mb-4">Select a student to view their skill gap analysis.</p>
                        <div class="row g-3">
                            <t t-foreach="state.studentList" t-as="st" t-key="st.id">
                                <div class="col-lg-3 col-md-4 col-sm-6">
                                    <div class="card shadow-sm h-100 cursor-pointer ga-student-card"
                                         t-on-click="() => this.selectStudent(st.id)">
                                        <div class="card-body text-center">
                                            <div class="rounded-circle bg-primary bg-opacity-10 d-inline-flex align-items-center justify-content-center mb-2" style="width:48px;height:48px">
                                                <i class="fa fa-user text-primary"/>
                                            </div>
                                            <h6 class="card-title mb-0" t-esc="st.name"/>
                                            <small class="text-muted" t-esc="st.email"/>
                                        </div>
                                    </div>
                                </div>
                            </t>
                        </div>
                    </t>

                    <!-- Gap Analysis View -->
                    <t t-elif="state.student">
                        <div class="d-flex align-items-center mb-4">
                            <button class="btn btn-sm btn-outline-secondary me-3" t-on-click="goBack"
                                    t-if="!this.props.action?.context?.student_id">
                                <i class="fa fa-arrow-left me-1"/> Back
                            </button>
                            <div>
                                <h2 class="mb-0">Gap Analysis</h2>
                                <small class="text-muted">
                                    Student: <strong t-esc="state.student.name"/>
                                    <t t-if="state.gapProfile">
                                        — Source: <span class="badge bg-secondary" t-esc="state.gapProfile.source_type"/>
                                    </t>
                                </small>
                            </div>
                        </div>

                        <t t-if="!state.gapProfile">
                            <div class="alert alert-info">
                                <i class="fa fa-info-circle me-2"/>
                                No gap profile found for this student. A placement test or exam is required first.
                            </div>
                        </t>

                        <t t-else="">
                            <!-- Skill Gap Bars -->
                            <div class="card shadow-sm mb-4">
                                <div class="card-header bg-white">
                                    <h5 class="mb-0">
                                        <i class="fa fa-bar-chart me-2 text-primary"/>
                                        Skill Gaps
                                    </h5>
                                </div>
                                <div class="card-body">
                                    <t t-if="state.skillGaps.length">
                                        <t t-foreach="state.skillGaps" t-as="gap" t-key="gap.skill">
                                            <div class="ga-bar-row mb-3">
                                                <div class="d-flex justify-content-between align-items-center mb-1">
                                                    <span class="fw-bold text-capitalize" t-esc="gap.skill"/>
                                                    <span class="small text-muted">
                                                        <t t-esc="gap.current"/> / <t t-esc="gap.target"/>
                                                    </span>
                                                </div>
                                                <div class="ga-bar-track">
                                                    <div class="ga-bar-fill"
                                                         t-att-class="barColor(gap.current, gap.target)"
                                                         t-att-style="'width:' + barWidth(gap.current, gap.target) + '%'">
                                                        <span class="ga-bar-label" t-if="barWidth(gap.current, gap.target) > 15">
                                                            <t t-esc="barWidth(gap.current, gap.target)"/>%
                                                        </span>
                                                    </div>
                                                </div>
                                            </div>
                                        </t>
                                    </t>
                                    <t t-else="">
                                        <p class="text-muted mb-0">No skill gap data available.</p>
                                    </t>
                                </div>
                            </div>

                            <div class="row g-4">
                                <!-- Weak Topics -->
                                <div class="col-lg-6">
                                    <div class="card shadow-sm h-100">
                                        <div class="card-header bg-white">
                                            <h5 class="mb-0">
                                                <i class="fa fa-warning me-2 text-warning"/>
                                                Weak Topics
                                            </h5>
                                        </div>
                                        <div class="card-body">
                                            <t t-if="state.weakTopics.length">
                                                <t t-foreach="state.weakTopics" t-as="topic" t-key="topic_index">
                                                    <div class="d-flex justify-content-between align-items-center py-2 border-bottom">
                                                        <span>
                                                            <i class="fa fa-circle text-danger me-2" style="font-size:0.5rem; vertical-align:middle"/>
                                                            <t t-esc="topic.name || topic"/>
                                                        </span>
                                                        <span class="badge bg-danger bg-opacity-75" t-if="topic.score !== undefined">
                                                            <t t-esc="topic.score"/>%
                                                        </span>
                                                    </div>
                                                </t>
                                            </t>
                                            <t t-else="">
                                                <p class="text-muted text-center mb-0 py-3">
                                                    <i class="fa fa-check-circle text-success me-1"/>
                                                    No weak topics identified
                                                </p>
                                            </t>
                                        </div>
                                    </div>
                                </div>

                                <!-- Recommendations -->
                                <div class="col-lg-6">
                                    <div class="card shadow-sm h-100">
                                        <div class="card-header bg-white">
                                            <h5 class="mb-0">
                                                <i class="fa fa-lightbulb-o me-2 text-info"/>
                                                Recommended Actions
                                            </h5>
                                        </div>
                                        <div class="card-body">
                                            <t t-foreach="state.recommendations" t-as="rec" t-key="rec_index">
                                                <div class="d-flex align-items-start py-2 border-bottom">
                                                    <div class="rounded-circle d-flex align-items-center justify-content-center me-3 flex-shrink-0"
                                                         t-att-class="'bg-' + rec.color + ' bg-opacity-10'"
                                                         style="width:36px; height:36px">
                                                        <i class="fa" t-att-class="rec.icon + ' text-' + rec.color"/>
                                                    </div>
                                                    <span class="small" t-esc="rec.text"/>
                                                </div>
                                            </t>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </t>
                    </t>

                    <t t-elif="!state.studentId">
                        <div class="text-center py-5 text-muted">
                            <i class="fa fa-line-chart fa-3x mb-3 d-block"/>
                            <p>No student selected. Open from a student record or select one above.</p>
                        </div>
                    </t>
                </div>
            </div>
        </Layout>
    </t>
</templates>
