
    aja                        d Z ddlZddlZddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZmZ ddlmZ ddlmZ  ej"                  ej$                  d	
        ej&                  d      Z eddd      Zej-                  e
g ddgdg       dedefdZej3                  d      d        ZdZej3                  d      d        Zd Zej=                  d      defd       Zy)u[  
Scheduler Service — FastAPI + Graphene GraphQL application.

A Python GraphQL API that replicates the PHP GraphQL scheduler widget endpoints.
Supports dynamic per-request database connections via the X-DB-Name header,
and JWT / X-Token authentication.

GraphQL endpoint: POST /
GraphiQL IDE:     GET  / (browser)
Health check:     GET  /health
    N)Optional)FastAPIRequestHTTPException)CORSMiddleware)HTMLResponseJSONResponse)schema)authenticatez1%(asctime)s [%(levelname)s] %(name)s: %(message)s)levelformat	schedulerzMYL Scheduler GraphQL APIz-Python GraphQL API for league game schedulingz1.0.0)titledescriptionversionGETPOST*)allow_originsallow_methodsallow_headersrequestreturnc                 P  K   d}| j                   dk(  rd}nd| j                   dk(  rU| j                  j                  dd      }d|v r5	 | j                          d{   }|j                  d	d      }d
|v sd|v rd}|r| dddS | j                  j                  d      xsU | j                  j                  d      xs8 | j                  j                  d      xs | j                  j                  d      }|st        dd      t        |       }| ||dS 7 # t        $ r Y w xY ww)z
    Extract and validate the X-DB-Name header and authenticate the user.
    The returned dict is available as `info.context` in all resolvers.
    Bypasses header and auth requirements for introspection and playground GET requests.
    Fr   Tr   content-type zapplication/jsonNquery__schemaIntrospectionQuery)r   db_nameuserzX-Organisationz	X-DB-Namezdb-namezX-DB  z1Organisation header (X-Organisation) is required.status_codedetail)methodheadersgetjson	Exceptionr   r   )r   is_introspectioncontent_typebodyr   r    r!   s          //var/www/html/myl_app/scheduler-service/main.pyget_contextr/   6   sM     ~~	6	!**>2>-$\\^+"-&*>%*G'+$ 
 	
 	,- 	'??{+	'??y)	' ??v&	  F
 	
  D  = ,  s=   AD&D D D ;BD&D 	D# D&"D##D&z/healthc                  
    ddiS )z;Simple health-check endpoint for the Laravel proxy to ping.statusok r3       r.   healthr5   l   s     dr4   a(  
<!DOCTYPE html>
<html>
<head>
  <title>GraphiQL</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/graphiql/3.0.6/graphiql.min.css" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/graphiql/3.0.6/graphiql.min.js"></script>
</head>
<body style="margin: 0; overflow: hidden;">
  <div id="graphiql" style="height: 100vh;"></div>
  <script>
    const fetcher = GraphiQL.createFetcher({ url: '/' });
    ReactDOM.render(
      React.createElement(GraphiQL, { fetcher: fetcher }),
      document.getElementById('graphiql')
    );
  </script>
</body>
</html>
/c                  *   K   t        t              S w)zServe the GraphiQL playground.content)r   GRAPHIQL_HTMLr3   r4   r.   graphql_playgroundr;      s      ..s   c                     |j                  d      }|d   dk7  ry| }|dd D ]  }||vs||   i ||<   ||   } |||d   <   y)zBHelper to inject mapped files at specified nested variables paths..r   	variablesN   )split)r>   pathvaluepartscurrparts         r.   set_value_at_pathrG      sj    JJsOEQx;Da ttDz1DJDz DrOr4   c                 L  K   | j                   j                  dd      }t        |        d{   }d|v r| j                          d{   }|j                  d      }|j                  d      }|r|st	        dd	      t        j                  |      }t        j                  |      }|j                  d
      xs i }|j                         D ]-  \  }	}
|j                  |	      }|s|
D ]  }t        |||        / |j                  d      }|j                  d      }nL	 | j                          d{   }|j                  d      }|j                  d
      }|j                  d      }t        j                  ||||       d{   }i }|j                  |j                  |d<   |j                  r6|j                  D cg c]  }dt        |j                  xs |      i c}|d<   t!        |      S 7 7 7 # t        $ r t	        dd	      w xY w7 c c}w w)z2Handle GraphQL queries and mutations via Graphene.r   r   Nzmultipart/form-data
operationsmapr"   z!Invalid multipart GraphQL requestr#   r>   r   operationNamezInvalid JSON body)variable_valuesoperation_namecontext_valuedatamessageerrorsr8   )r'   r(   r/   formr   r)   loadsitemsrG   r*   r
   execute_asyncrO   rQ   stroriginal_errorr	   )r   r,   contextrR   operations_strmap_strrI   map_datar>   file_keypaths	file_itemrB   r   rM   r-   resultresponse_dataerrs                      r.   graphql_endpointrb      s     ??&&~r:L  ((G,\\^#,/((5/WC8[\\ZZ/
::g&NN;/52	  (~~/ 	BOHe*I! BD%iyAB	B w'#8	M 'D !HH[)	/2 ''!%	 F M{{ &f}}Z`ZgZg"hSVIs33E3E3L/M#N"hh.._ ) $. ( 	MC8KLL	M #isq   +H$G;H$	G>
BH$*9H$$H 7H8H <AH$H;H$"H)H$>H$H HH$H$) __doc__r)   loggingtypingr   fastapir   r   r   fastapi.middleware.corsr   fastapi.responsesr   r	   r
   authr   basicConfigINFO	getLoggerloggerappadd_middlewaredictr/   r(   r5   r:   r;   rG   postrb   r3   r4   r.   <module>rr      s  
    3 3 2 8  
   
,,> 
		;	'
 
%?   &/%	  /w /4 /l  . / /

 #4/G 4/ 4/r4   