
    rilI                        S SK r S SKrS SKrS SKJr  \" 5         S SKrS SKJr  S SKJ	r	  S SK
Jr  \R                  " \5      R                  5       R                  R                  r\S-  S-  r\R%                  SSS	9  \S
-  r\R(                  " SS5      R+                  5       S;   r\R(                  " S5      =(       d    \R.                  " S5      r " S S\	5      r\" \R4                  R7                  \SS5      S9rS rS rS rS r g)    N)load_dotenv)Jinja2Templates)StaticFiles)ResponseappdatabaseT)parentsexist_okz
db.sqlite3APP_ENVdev)prod
productionAPP_SECRET_KEY@   c                   0   ^  \ rS rSrS\4U 4S jjrSrU =r$ )CachedStaticFiles   returnc                 F   > [         TU ]  " U0 UD6nSUR                  S'   U$ )Nz#public, max-age=31536000, immutablezCache-Control)superfile_responseheaders)selfargskwargsresp	__class__s       3/home/mario/Escritorio/thel4footbal/app/database.pyr   CachedStaticFiles.file_response   s*    w$d5f5(M_%     )__name__
__module____qualname____firstlineno__r   r   __static_attributes____classcell__)r   s   @r   r   r      s      r    r   	templates)	directoryc                  v   [         R                  " [        S SS9n [         R                  U l        U R                  S5        U R                  S5        U R                  S5        U R                  S5        U R                  S5        U R                  S5         U R                  S	5        U $ ! [         a     U $ f = f)
NF)isolation_levelcheck_same_threadzPRAGMA foreign_keys = ON;zPRAGMA journal_mode = WAL;zPRAGMA synchronous = NORMAL;zPRAGMA busy_timeout = 5000;zPRAGMA cache_size = -20000;zPRAGMA temp_store = MEMORY;zPRAGMA mmap_size = 268435456;)sqlite3connectDB_PATHRowrow_factoryexecute	Exception)conns    r   get_connr5      s    ??7DERD{{DLL,-LL-.LL/0LL./LL./LL./45 K  Ks   B* *
B87B8c                      [        5       $ )N)r5   r!   r    r   _connr7   .   s
    :r    c                      U R                  SU S35      R                  5        Vs1 s H  o"S   iM	     sn$ s  snf ! [         a    [        5       s $ f = f)NzPRAGMA table_info()   )r2   fetchallr3   set)r4   tablers      r   _table_columnsr?   1   sU    "ll-?wa+HIRRTUT!TUUU us   %= 8= = AAc                     [        5        n U R                  5       nUR                  S5        UR                  S5        UR                  S5      R                  5        Vs1 s H  o"S   iM	     nnSU;  a  UR                  S5        SU;  a  UR                  S5        S	U;  a  UR                  S
5        SU;  a  UR                  S5        UR                  S5        UR                  S5      R                  5        Vs1 s H  o"S   iM	     nnSU;  a#  UR                  S5         UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5      R                  5        Vs1 s H  o"S   iM	     nnSU;  a"  UR                  S5        UR                  S5        SU;  a  UR                  S5        SU;  a  UR                  S 5        S!U;  a  UR                  S"5        S#U;  a  UR                  S$5        S%U;  a  UR                  S&5        S	U;  a  UR                  S'5        S(U;  a  UR                  S)5        S*U;  a  UR                  S+5        S,U;  a  UR                  S-5        S.U;  a  UR                  S/5        S0U;  a  UR                  S15        UR                  S25        UR                  S35      R                  5        Vs1 s H  o"S   iM	     nnS0U;  a  UR                  S45        UR                  S55        UR                  S65        UR                  S75        UR                  S85        UR                  S95        UR                  S:5        UR                  S;5        UR                  S<5        UR                  S=5        UR                  S>5        UR                  S?5        UR                  S@5        UR                  SA5        [        U SB5      nSCU;  a  UR                  SD5        SEU;  a  UR                  SF5        UR                  SG5        UR                  SH5        [        U SI5      nSJU;  a  UR                  SK5        SLU;  a  UR                  SM5        SNU;  a  UR                  SO5        UR                  SP5        UR                  SQ5        UR                  SR5        UR                  SS5        UR                  ST5        UR                  SU5        UR                  SV5        UR                  SW5        UR                  SX5        UR                  SY5        UR                  SZ5        UR                  S[5        UR                  S\5        UR                  S]5        UR                  S^5        UR                  S_5        U R                  5         S S S 5        g s  snf s  snf ! [         a     GNf = fs  snf s  snf ! , (       d  f       g = f)`NzPRAGMA foreign_keys=ONa  
        CREATE TABLE IF NOT EXISTS users (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            email TEXT NOT NULL UNIQUE,
            name TEXT NOT NULL,
            first_name TEXT,
            last_name TEXT,
            phone TEXT,
            password_hash TEXT NOT NULL,
            role TEXT NOT NULL DEFAULT 'alumno',
            created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%S','now','localtime'))
        )
        zPRAGMA table_info(users)r:   
first_namez,ALTER TABLE users ADD COLUMN first_name TEXT	last_namez+ALTER TABLE users ADD COLUMN last_name TEXTphonez'ALTER TABLE users ADD COLUMN phone TEXTis_mvp90z@ALTER TABLE users ADD COLUMN is_mvp90 INTEGER NOT NULL DEFAULT 0aJ  
        CREATE TABLE IF NOT EXISTS user_profile (
            user_id INTEGER PRIMARY KEY,
            phone TEXT,
            avatar TEXT,
            city TEXT,
            dni TEXT,
            birth_date TEXT,
            gender TEXT,
            FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
        )
        zPRAGMA table_info(user_profile)genderz/ALTER TABLE user_profile ADD COLUMN gender TEXTa1  
                    UPDATE user_profile 
                    SET gender = (
                        SELECT sexo FROM estadisticas_jugadores 
                        WHERE estadisticas_jugadores.user_id = user_profile.user_id
                    )
                    WHERE gender IS NULL
                a  
        CREATE TABLE IF NOT EXISTS user_sports (
            user_id INTEGER PRIMARY KEY,
            team TEXT,
            category TEXT,
            position TEXT,
            dominant_foot TEXT,
            strengths TEXT,
            weaknesses TEXT,
            injury_history TEXT,
            training_type TEXT,
            FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
        )
        a  
        CREATE TABLE IF NOT EXISTS user_health (
            user_id INTEGER PRIMARY KEY,
            physical_work INTEGER,
            physical_work_details TEXT,
            smoking INTEGER,
            alcohol INTEGER,
            recovery TEXT,
            chest_pain INTEGER,
            discomfort INTEGER,
            FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
        )
        ap  
        CREATE TABLE IF NOT EXISTS user_consent (
            user_id INTEGER PRIMARY KEY,
            whatsapp_content INTEGER,
            video_permission INTEGER,
            privacy_acceptance INTEGER,
            data_confirmation INTEGER,
            agreement INTEGER,
            FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
        )
        a0  
        CREATE TABLE IF NOT EXISTS user_guardians (
            user_id INTEGER PRIMARY KEY,
            occupation TEXT,
            study_place TEXT,
            parent_name TEXT,
            parent_email TEXT,
            FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
        )
        a9  
        CREATE TABLE IF NOT EXISTS user_marketing (
            user_id INTEGER PRIMARY KEY,
            found_us TEXT,
            enjoyment TEXT,
            nerves_confidence TEXT,
            additional_comments TEXT,
            FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
        )
        z
        CREATE TABLE IF NOT EXISTS user_finance (
            user_id INTEGER PRIMARY KEY,
            matricula_eur REAL NOT NULL DEFAULT 0,
            FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
        )
        a  
        CREATE TABLE IF NOT EXISTS reservations (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%S','now','localtime')),
            date TEXT NOT NULL,
            time TEXT,
            duration_minutes INTEGER,
            price_cents INTEGER NOT NULL DEFAULT 0,
            name TEXT,
            email TEXT,
            phone TEXT,
            notes TEXT,
            user_id INTEGER,
            status TEXT NOT NULL DEFAULT 'confirmada',
            docente_id INTEGER NOT NULL,
            paid INTEGER NOT NULL DEFAULT 0,
            FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE SET NULL,
            FOREIGN KEY(docente_id) REFERENCES users(id) ON DELETE CASCADE
        )
        zPRAGMA table_info(reservations)
created_atz3ALTER TABLE reservations ADD COLUMN created_at TEXTzmUPDATE reservations SET created_at = strftime('%Y-%m-%dT%H:%M:%S','now','localtime') WHERE created_at IS NULLtimez-ALTER TABLE reservations ADD COLUMN time TEXTduration_minutesz<ALTER TABLE reservations ADD COLUMN duration_minutes INTEGERprice_centszJALTER TABLE reservations ADD COLUMN price_cents INTEGER NOT NULL DEFAULT 0namez-ALTER TABLE reservations ADD COLUMN name TEXTemailz.ALTER TABLE reservations ADD COLUMN email TEXTz.ALTER TABLE reservations ADD COLUMN phone TEXTnotesz.ALTER TABLE reservations ADD COLUMN notes TEXTuser_idz3ALTER TABLE reservations ADD COLUMN user_id INTEGERstatuszMALTER TABLE reservations ADD COLUMN status TEXT NOT NULL DEFAULT 'confirmada'
docente_idzIALTER TABLE reservations ADD COLUMN docente_id INTEGER NOT NULL DEFAULT 1paidzCALTER TABLE reservations ADD COLUMN paid INTEGER NOT NULL DEFAULT 0a  
        CREATE TABLE IF NOT EXISTS reservation_students (
            reservation_id INTEGER NOT NULL,
            user_id INTEGER NOT NULL,
            paid INTEGER NOT NULL DEFAULT 0,
            PRIMARY KEY (reservation_id, user_id),
            FOREIGN KEY(reservation_id) REFERENCES reservations(id) ON DELETE CASCADE,
            FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
        )
        z'PRAGMA table_info(reservation_students)zKALTER TABLE reservation_students ADD COLUMN paid INTEGER NOT NULL DEFAULT 0aS  
        CREATE TABLE IF NOT EXISTS jugadores_gastos (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%S','now','localtime')),
            date TEXT NOT NULL,
            user_id INTEGER,
            reservation_id INTEGER,
            amount_cents INTEGER NOT NULL,
            category TEXT,
            concept TEXT,
            notes TEXT,
            FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE,
            FOREIGN KEY(reservation_id) REFERENCES reservations(id) ON DELETE CASCADE
        )
        z@CREATE INDEX IF NOT EXISTS idx_jg_date ON jugadores_gastos(date)zCCREATE INDEX IF NOT EXISTS idx_jg_user ON jugadores_gastos(user_id)zQCREATE INDEX IF NOT EXISTS idx_jg_reservation ON jugadores_gastos(reservation_id)a  
        CREATE TABLE IF NOT EXISTS notifications (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%S','now','localtime')),
            title TEXT,
            body TEXT,
            sender_id INTEGER,
            recipient_id INTEGER NOT NULL,
            reservation_id INTEGER,
            is_read INTEGER NOT NULL DEFAULT 0,
            deleted_by_sender INTEGER NOT NULL DEFAULT 0,
            deleted_by_recipient INTEGER NOT NULL DEFAULT 0,
            FOREIGN KEY(sender_id) REFERENCES users(id) ON DELETE SET NULL,
            FOREIGN KEY(recipient_id) REFERENCES users(id) ON DELETE CASCADE,
            FOREIGN KEY(reservation_id) REFERENCES reservations(id) ON DELETE CASCADE
        )
        zPCREATE INDEX IF NOT EXISTS idx_notif_rec ON notifications(recipient_id, is_read)zECREATE INDEX IF NOT EXISTS idx_notif_send ON notifications(sender_id)zICREATE INDEX IF NOT EXISTS idx_notif_created ON notifications(created_at)a  
        CREATE TABLE IF NOT EXISTS player_ratings (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            player_id INTEGER NOT NULL,
            docente_id INTEGER NOT NULL,
            rating INTEGER NOT NULL,
            notes TEXT,
            created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%S','now','localtime')),
            FOREIGN KEY(player_id) REFERENCES users(id) ON DELETE CASCADE,
            FOREIGN KEY(docente_id) REFERENCES users(id) ON DELETE CASCADE
        )
        a  
        CREATE TABLE IF NOT EXISTS valoraciones_jugador (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            eval_id TEXT NOT NULL,
            player_id INTEGER NOT NULL,
            docente_id INTEGER NOT NULL,
            area TEXT NOT NULL,
            subescala TEXT NOT NULL,
            indicador TEXT NOT NULL,
            score INTEGER NOT NULL,
            observaciones TEXT,
            created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%S','now','localtime')),
            FOREIGN KEY(player_id) REFERENCES users(id) ON DELETE CASCADE,
            FOREIGN KEY(docente_id) REFERENCES users(id) ON DELETE CASCADE
        )
        a  
        CREATE TABLE IF NOT EXISTS "formulario-registro" (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            pin TEXT NOT NULL UNIQUE,
            email TEXT,
            name TEXT,
            used INTEGER NOT NULL DEFAULT 0,
            expires_at TEXT NOT NULL,
            created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%S','now','localtime'))
        )
        zZCREATE INDEX IF NOT EXISTS idx_formreg_used_exp ON "formulario-registro"(used, expires_at)a  
        CREATE TABLE IF NOT EXISTS academy_courses (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            title TEXT NOT NULL,
            description TEXT,
            thumbnail TEXT,
            status TEXT NOT NULL DEFAULT 'draft',
            created_by INTEGER NOT NULL,
            created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%S','now','localtime')),
            updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%S','now','localtime')),
            order_index INTEGER NOT NULL DEFAULT 0,
            resource_url TEXT,
            resource_name TEXT,
            FOREIGN KEY(created_by) REFERENCES users(id) ON DELETE CASCADE
        )
        academy_coursesresource_urlz8ALTER TABLE academy_courses ADD COLUMN resource_url TEXTresource_namez9ALTER TABLE academy_courses ADD COLUMN resource_name TEXTa*  
        CREATE TABLE IF NOT EXISTS academy_sections (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            course_id INTEGER NOT NULL,
            title TEXT NOT NULL,
            description TEXT,
            order_index INTEGER NOT NULL DEFAULT 0,
            created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%S','now','localtime')),
            updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%S','now','localtime')),
            FOREIGN KEY(course_id) REFERENCES academy_courses(id) ON DELETE CASCADE
        )
        a@  
        CREATE TABLE IF NOT EXISTS academy_lessons (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            course_id INTEGER NOT NULL,
            section_id INTEGER,
            title TEXT NOT NULL,
            description TEXT,
            content_type TEXT NOT NULL,
            content_data TEXT,
            duration_minutes INTEGER,
            order_index INTEGER NOT NULL DEFAULT 0,
            status TEXT NOT NULL DEFAULT 'draft',
            created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%S','now','localtime')),
            updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%S','now','localtime')),
            FOREIGN KEY(course_id) REFERENCES academy_courses(id) ON DELETE CASCADE,
            FOREIGN KEY(section_id) REFERENCES academy_sections(id) ON DELETE SET NULL
        )
        academy_lessons
section_idzlALTER TABLE academy_lessons ADD COLUMN section_id INTEGER REFERENCES academy_sections(id) ON DELETE SET NULLattachment_urlz:ALTER TABLE academy_lessons ADD COLUMN attachment_url TEXTattachment_namez;ALTER TABLE academy_lessons ADD COLUMN attachment_name TEXTa  
        CREATE TABLE IF NOT EXISTS academy_enrollments (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            user_id INTEGER NOT NULL,
            course_id INTEGER NOT NULL,
            enrolled_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%S','now','localtime')),
            completed_at TEXT,
            UNIQUE(user_id, course_id),
            FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE,
            FOREIGN KEY(course_id) REFERENCES academy_courses(id) ON DELETE CASCADE
        )
        aE  
        CREATE TABLE IF NOT EXISTS academy_progress (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            user_id INTEGER NOT NULL,
            lesson_id INTEGER NOT NULL,
            completed INTEGER NOT NULL DEFAULT 0,
            completed_at TEXT,
            last_accessed_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%S','now','localtime')),
            UNIQUE(user_id, lesson_id),
            FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE,
            FOREIGN KEY(lesson_id) REFERENCES academy_lessons(id) ON DELETE CASCADE
        )
        zUCREATE INDEX IF NOT EXISTS idx_academy_sections_course ON academy_sections(course_id)zSCREATE INDEX IF NOT EXISTS idx_academy_lessons_course ON academy_lessons(course_id)zUCREATE INDEX IF NOT EXISTS idx_academy_lessons_section ON academy_lessons(section_id)zWCREATE INDEX IF NOT EXISTS idx_academy_enrollments_user ON academy_enrollments(user_id)z[CREATE INDEX IF NOT EXISTS idx_academy_enrollments_course ON academy_enrollments(course_id)zQCREATE INDEX IF NOT EXISTS idx_academy_progress_user ON academy_progress(user_id)zUCREATE INDEX IF NOT EXISTS idx_academy_progress_lesson ON academy_progress(lesson_id)zAINSERT OR IGNORE INTO user_profile(user_id)  SELECT id FROM userszAINSERT OR IGNORE INTO user_sports(user_id)   SELECT id FROM userszAINSERT OR IGNORE INTO user_health(user_id)   SELECT id FROM userszAINSERT OR IGNORE INTO user_consent(user_id)  SELECT id FROM userszAINSERT OR IGNORE INTO user_guardians(user_id)SELECT id FROM userszAINSERT OR IGNORE INTO user_marketing(user_id)SELECT id FROM userszTINSERT OR IGNORE INTO user_finance(user_id, matricula_eur) SELECT id, 0.0 FROM users)r7   cursorr2   r;   r3   r?   commit)	r4   cr>   
cols_userscols_profilecols_rescols_rscols_coursescols_lessonss	            r   init_dbra   7   s   	DKKM			*+			  	 %&II.H$I$R$R$TU$Tqd$T
Uz)IIDEj(IICD*$II?@Z'IIXY			  	 '(ii0Q&R&[&[&]^&]!&]^<'IIGH
		   	
		  	 	
		  	 	
		 
 
	 	
		 	 		 	
		 	 		 	
		  	 	
		  	( #$)),M"N"W"W"YZ"YQaD"YZx'IIKLII  F  G!IIEFX-IITU(IIbc!IIEF("IIFG("IIFG("IIFGH$IIKL8#IIefx'IIab!II[\			 	 		 "#+T!U!^!^!`a!`AQ4!`a IIcd			  	 	
		TU			WX			ef			  	" 	
		de			YZ			]^			  	 	
		  	" 	
		 
 
	 	
		no 	
		  	$ &d,=>-IIPQ,.IIQR			  	 	
		  	( &d,=>|+II  E  F </IIRSL0IIST			  	 	
		  	 	
		ij			gh			ij			kl			op			ef			ij 	
		UV			UV			UV			UV			UV			UV			hi} 
" V, _  | [J bo 
sj   AX4X-BX4=XX4%X6BX4X*EX45X/KX4
X4
X'#X4&X''X44
Y)!r-   pathlibosdotenvr   secretsfastapi.templatingr   fastapi.staticfilesr   fastapir   Path__file__resolveparentBASE_DIRDB_DIRmkdirr/   getenvlowerIS_PRODtoken_urlsafe
SECRET_KEYr   pathjoinr(   r5   r7   r?   ra   r!   r    r   <module>rw      s      	    . + <<!))+2299	E	J	& TD )
<

))Iu
%
+
+
-1G
GYY'(EG,A,A",E
  bggll8UK&PQ	r    