using System;
using Microsoft.Data.Sqlite;

class Program
{
    static void Main()
    {
        using var conn = new SqliteConnection("Data Source=students.db;");
        conn.Open();

        void Exec(string sql)
        {
            using var cmd = conn.CreateCommand();
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();
        }

        // --- DDL + data (vložit SQL bloky z části #2) ---
        Exec(@"
PRAGMA foreign_keys = ON;
DROP TABLE IF EXISTS StudentChoice; DROP TABLE IF EXISTS Friendship;
DROP TABLE IF EXISTS Subject; DROP TABLE IF EXISTS Student;

CREATE TABLE Student (id INTEGER PRIMARY KEY, full_name TEXT NOT NULL, abbrev TEXT);
CREATE TABLE Subject (id INTEGER PRIMARY KEY, code TEXT NOT NULL UNIQUE, name TEXT NOT NULL);
CREATE TABLE Friendship (student_id INTEGER NOT NULL, friend_id INTEGER NOT NULL,
  PRIMARY KEY(student_id, friend_id),
  FOREIGN KEY (student_id) REFERENCES Student(id) ON DELETE CASCADE,
  FOREIGN KEY (friend_id)  REFERENCES Student(id) ON DELETE CASCADE);
CREATE TABLE StudentChoice (student_id INTEGER NOT NULL, pref_order INTEGER NOT NULL CHECK(pref_order IN (1,2)),
  subject_id INTEGER NOT NULL,
  PRIMARY KEY(student_id, pref_order),
  FOREIGN KEY (student_id) REFERENCES Student(id) ON DELETE CASCADE,
  FOREIGN KEY (subject_id) REFERENCES Subject(id) ON DELETE RESTRICT);

INSERT INTO Student (id, full_name, abbrev) VALUES
(1,'Petr Zahradil','Za'),
(2,'Antonín Neužil','Ne'),
(3,'Eva Kárná','Ka'),
(4,'Pavel Troup','Tr'),
(5,'Jan Zavadil','Za'),
(6,'Irina Kornienko','Ko'),
(7,'Sabina Křížová','Kr');

INSERT INTO Subject (id, code, name) VALUES
(1,'MA','Matematika'),(2,'DEJ','Dějepis'),(3,'ANG','Angličtina'),
(4,'TV','Tělesná výchova'),(5,'OS','Operační systémy'),
(6,'NET','Počítačové sítě'),(7,'PRG','Programování');

INSERT INTO Friendship VALUES
(1,2),(1,3),(2,6),(2,7),(3,4),(3,1),(4,3),(4,1),(5,1),(5,3),(6,4),(6,2),(6,3),(7,2),(7,3);

INSERT INTO StudentChoice VALUES
(1,1,1),(1,2,2),
(2,1,3),(2,2,4),
(3,1,5),(3,2,1),
(4,1,5),(4,2,6),
(5,1,7),(5,2,3),
(6,1,7),(6,2,4),
(7,1,7),(7,2,5);
");

        string sql = @"
WITH FriendsAgg AS (
  SELECT f.student_id, GROUP_CONCAT(s2.abbrev, ', ') AS friends_abbrev
  FROM Friendship f
  JOIN Student s2 ON s2.id = f.friend_id
  GROUP BY f.student_id
),
Choices AS (
  SELECT
    sc.student_id,
    MAX(CASE WHEN sc.pref_order=1 THEN sub.code END) AS VolitPr1,
    MAX(CASE WHEN sc.pref_order=2 THEN sub.code END) AS VolitPr2
  FROM StudentChoice sc
  JOIN Subject sub ON sub.id = sc.subject_id
  GROUP BY sc.student_id
)
SELECT
  s.full_name   AS Jmeno,
  s.abbrev      AS Zkratka,
  IFNULL(fa.friends_abbrev,'') AS Pratele,
  c.VolitPr1,
  c.VolitPr2
FROM Student s
LEFT JOIN FriendsAgg fa ON fa.student_id = s.id
LEFT JOIN Choices   c  ON c.student_id  = s.id
ORDER BY s.id;";

using var cmd2 = conn.CreateCommand();
cmd2.CommandText = sql;
using var rdr = cmd2.ExecuteReader();

Console.WriteLine("Jméno | Zkratka | Přátelé | VolitPr1 | VolitPr2");
while (rdr.Read())
{
    Console.WriteLine(
        $"{rdr.GetString(0)} | {rdr.GetString(1)} | {rdr.GetString(2)} | {rdr.GetString(3)} | {rdr.GetString(4)}"
    );
}
    }
}
