
    k)i~              
          d dl mZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZ d dlmZ d dlmZ d d	lmZ  e       Zej+                  d
de
       ee       edd       edd      fdededede
fd       Zej1                  d
de
       ee       ee      fdededede
fd       Zej1                  dde       ee       ee       edd      fdedededefd        Zy)!    )Query)	APIRouterDepends)Session)get_session)BaseResponseSuccessResponse)get_current_user)
UserSchema)SiteMasterCreateRequestSchema)services/zFetch All Master Data)nameresponse_model z`Specific master data to be fetched by its key as a comma separated list. Eg: ?keys=country,state)defaultdescriptionNzAAdditional related fields that are requested. Could be any of ...dbkeyssearchreturnc                    K   t        j                  | ||       d{   }t        d|       t        |d      S 7 w)a  
    Call this API to list all available master data of requesting user

    Query Param 'keys' is a comma separated string of master data ids. Eg: ?keys=business_profile,job_type

    On successfull execution the API would return '200 OK' JSON response of the following format
    ```
    {
        "data": {
            "business_locale": [
                {
                    "value": "en_US",
                    "key": "business_locale",
                    "group": "Locale",
                    "parent_id": null,
                    "id": 13,
                    "label": "United States",
                    "display_order": 0
                }
            ],
            "business_region": [
                {
                    "value": "US",
                    "key": "business_region",
                    "group": "Locale",
                    "parent_id": null,
                    "id": 17,
                    "label": "United States",
                    "display_order": 0
                }
            ]
        },
        "status_code": 200,
        "success": true,
        "message": "Request handled successfully"
    }
    ```
    )r   r   r   Nz---fetch data----zSite Master Datadatamessage)r   list_master_entriesprintr   )r   r   r   responses       Z/var/www/html/hwPaymentPortal-be-dev/hw-payment-portal-api/src/apps/site_masters/router.pyget_master_entriesr       sE     f 11D H 

x(X/ABB	s   ><>zCreate Master Data Entrypayloadcurrent_userc                 h   K   t        j                  || |       d{   }t        |d      S 7 w)a  
    Call this API to create a master data entry

    On successfull execution the API would return '200 OK' JSON response of the following format
    ```
    {
        "data": {
            "original_name": "avt.png",
            "name": "avt_211028_163403.png",
            "path": "/uploads/avt_211028_163403.png",
            "file_type": "document",
            "mime": "image/png",
            "cdn_id": null,
            "id": 15,
            "created_at": "2021-10-28T16:34:03.005096+00:00",
            "full_url": "http://localhost/uploads/avt_211028_163403.png",
            "created_by": {...}
        },
        "status_code": 200,
        "success": true,
        "message": "Request handled successfully"
    }
    ```
    )r   r!   
created_byNzData created successfullyr   )r   create_master_entryr	   )r!   r   r"   r   s       r   create_master_data_entryr&   I   s9     < 11w< H 2MNNs   202z/seedz!Populate Default Site Master DataFzgForce population even if data already exists. If false, only populates if table is empty or incomplete.forcec                    K   |rt        j                  |       }nt        j                  |       }|j                  dd      rt	        |d      S t	        |d|j                  dd             S w)a  
    Call this API to populate the SiteMaster table with default data from seeder
    
    This endpoint will:
    - Check existing data in the SiteMaster table
    - Insert default data from the seeder if missing
    - Provide detailed feedback about the operation
    
    Query Parameters:
    - force: If true, attempts to insert all default data regardless of existing records.
             If false (default), only populates if table has insufficient data.
    
    On successful execution, the API returns a '200 OK' JSON response:
    ```json
    {
        "data": {
            "success": true,
            "message": "Site master data population completed",
            "records_inserted": 50,
            "records_skipped": 10,
            "records_with_errors": 0,
            "total_processed": 60,
            "database_total_before": 10,
            "database_total_after": 60,
            "inserted_items": [
                "country -> US",
                "country -> CA",
                "..."
            ],
            "skipped_items": [
                "locale -> en_US",
                "..."
            ],
            "error_items": []
        },
        "status_code": 200,
        "success": true,
        "message": "Site master data seeding completed successfully"
    }
    ```
    
    Access Control:
    - Requires authentication
    - Only authenticated users can run this seeder
    successFz/Site master data seeding completed successfullyr   z!Site master data seeding failed: r   zUnknown error)r   !populate_default_site_master_dataseed_site_master_data_if_emptygetr	   )r   r"   r'   results       r   seed_default_master_datar.   m   sw     r ;;B? 88<zz)U#E
 	

 7

9o8^7_`
 	
s   A-A/)fastapi.param_functionsr   fastapir   r   sqlalchemy.orm.sessionr   src.core.databaser   src.apps.base.schemas.responsesr   r	   src.apps.auth.utils.authr
   "src.apps.users.schemas.user_commonr   2src.apps.site_masters.schemas.site_master_requestsr   src.apps.site_mastersr   routerr,   strr    postr&   boolr.        r   <module>r>      sp   ) & * ) H 5 9 \ * 
 C-lK+&v W6C6C
6C 6C 6C L6Cr S1,O +&&'78 O* O O  O 	 O P OF W>_+&&'78}H
H
H
 H
 H
 `H
r=   