import sys
import boto3
import json
import argparse
import logging

lfclient = boto3.client('lakeformation')
glueclient = boto3.client('glue')
print('boto3 version = ', boto3.__version__)
stsclient = boto3.client("sts")


catalog_id= stsclient.get_caller_identity()["Account"]
database_name= 'rl_bank_iceberg'
view_name= 'mdv_transaction1'
sql_query = "SELECT a.transaction_id, a.transaction_type, b.transaction_location \
FROM bankdata_icebergdb.transaction_table1 a \
RIGHT JOIN bankdata_icebergdb.transaction_table2 b \
ON a.transaction_id = b.transaction_id;"

try:
    response_createtable = glueclient.create_table(
        CatalogId=catalog_id,
        DatabaseName=database_name,
        TableInput={
            'Name': view_name,
            'Description': 'Spark View Cross account',
            'StorageDescriptor': {
                'Columns': [
                    {
                        'Name': 'transaction_id',
                        'Type': 'string'
                    },
                    {
                        'Name': 'transaction_type',
                        'Type': 'string'
                    },
                    {
                        'Name': 'transaction_location',
                        'Type': 'string'
                    }
                  ],
                'SerdeInfo': {}
            },
            'ViewDefinition': {
                'IsProtected': True,
                'Definer': 'arn:aws:iam::<your-producer-account-id>:role/Data-Analyst',
                'Representations': [
                    {
                        'Dialect': 'SPARK',
                        'DialectVersion': '1.0',
                        'ViewOriginalText': sql_query,
                        'ViewExpandedText': sql_query

                    },
                ],
                'SubObjects': [
                    'arn:aws:glue:<your-region>:<your-central-account-id>:table/bankdata_icebergdb/transaction_table1',
                    'arn:aws:glue:<your-region>:<your-central-account-id>:table/bankdata_icebergdb/transaction_table2'
                ]
            }
        }
    )
    print('response_createview = ',response_createtable)
except Exception as e:
    print(f"Error creating Glue View: {e}")

