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96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import logging
import os
from dataclasses import dataclass
from typing import List

from supabase import create_client


@dataclass
class NecklaceData:
    necklace_id: str
    necklace_url: str
    x_lean_offset: float
    y_lean_offset: float
    x_broad_offset: float
    y_broad_offset: float
    category: str


supabase_url = os.getenv("SUPABASE_URL")
supabase_key = os.getenv("SUPABASE_KEY")

supabase = create_client(supabase_url, supabase_key)


def fetch_necklace_offset_each_store(storename: str) -> List[NecklaceData]:
    response = supabase.table("MagicMirror").select("*").eq("StoreName", storename).execute()

    necklace_data_list = []

    for item in response.data:
        jewellery_url = (
            f"https://lvuhhlrkcuexzqtsbqyu.supabase.co/storage/v1/object/public/Stores/"
            f"{storename}/{item['Category']}/image/{item['Id']}.png"
        )

        necklace_data = NecklaceData(
            necklace_id=item['Id'],
            necklace_url=jewellery_url,
            x_lean_offset=item['x_lean'],
            y_lean_offset=item['y_lean'],
            x_broad_offset=item['x_chubby'],
            y_broad_offset=item['y_chubby'],
            category=item['Category']
        )

        necklace_data_list.append(necklace_data)

    return necklace_data_list


def fetch_model_body_type(image_url: str):
    try:
        response = supabase.table("JewelMirror_ModelImages").select("*").eq("image_url", image_url).execute()

        for item in response.data:
            print(item)
            return item['image_url'], item['body_structure']




    except Exception as e:
        print(f"The image url is not found in the table", {e})
        return None


def upload_information_to_new_table(necklace_id, nto_images_urls, cto_images_urls, mto_urls, video_urls, model_name):
    try:
        response_check = supabase.table("JM_Productpage").select("*").eq("Id", necklace_id).execute()

        if response_check.data:
            existing_record = response_check.data[0]

            updated_nto = f"{existing_record['nto_images_urls']},{nto_images_urls}" if existing_record[
                'nto_images_urls'] else nto_images_urls
            updated_cto = f"{existing_record['cto_images_urls']},{cto_images_urls}" if existing_record[
                'cto_images_urls'] else cto_images_urls
            updated_mto = f"{existing_record['mto_images_urls']},{mto_urls}" if existing_record[
                'mto_images_urls'] else mto_urls
            updated_video = f"{existing_record['video_urls']},{video_urls}" if existing_record[
                'video_urls'] else video_urls
            updated_model = f"{existing_record['model_name']},{model_name}" if existing_record[
                'model_name'] else model_name

            current_approve = existing_record.get('approve', {})
            if not isinstance(current_approve, dict):
                current_approve = {}

            if model_name not in current_approve:
                current_approve[model_name] = False

            logging.info(f"Updated model name: {updated_model}")
            logging.info(f"Updated approve status: {current_approve}")

            response = supabase.table("JM_Productpage").update({
                "nto_images_urls": updated_nto,
                "cto_images_urls": updated_cto,
                "mto_images_urls": updated_mto,
                "video_urls": updated_video,
                "model_name": updated_model,
                "approve": current_approve
            }).eq("Id", necklace_id).execute()

            print("Updated existing record")

        else:
            response = supabase.table("JM_Productpage").insert({
                "Id": necklace_id,
                "nto_images_urls": nto_images_urls,
                "cto_images_urls": cto_images_urls,
                "mto_images_urls": mto_urls,
                "video_urls": video_urls,
                "model_name": model_name,
                "approve": {
                    model_name: False
                }
            }).execute()

            print("Inserted new record")

        return response

    except Exception as e:
        print(f"Error in uploading the data to the table: {str(e)}")
        logging.error(f"Error in uploading the data to the table: {str(e)}")
        return None


def supabase_image_fetch_product_page(necklace_id, model_name):
    try:
        response = supabase.table("JM_Productpage").select("*").eq("Id", necklace_id).execute()

        if not response.data:
            print(f"No data found for necklace_id: {necklace_id}")
            return None

        # Get the first item from response
        item = response.data[0]

        def process_url_list(url_string):
            if not url_string:
                return []

            url_groups = url_string.replace("[", "").replace("]", "").split("],[")

            filtered_urls = []
            for group in url_groups:
                urls = [url.strip().strip('"\'') for url in group.split(",")]
                matching_urls = [url for url in urls if model_name in url]
                filtered_urls.extend(matching_urls)

            return filtered_urls

        try:
            nto_images = process_url_list(item.get('nto_images_urls', ''))
            cto_images = process_url_list(item.get('cto_images_urls', ''))
            mto_images = process_url_list(item.get('mto_images_urls', ''))
            print(f"nto_images: {nto_images}")
            print(f"cto_images: {cto_images}")
            print(f"mto_images: {mto_images}")

            video_urls = []
            if item.get('video_urls'):
                video_list = item['video_urls'].split(',')
                video_urls = [url.strip() for url in video_list if model_name in url]

            print(f"video_urls: {video_urls}")

        except AttributeError as e:
            print(f"Error parsing URLs: {e}")
            return None

        response = {
            'nto': nto_images,
            'cto': cto_images,
            'mto': mto_images,
            'video': video_urls,
            "status": "success"

        }

        return response
    except Exception as e:
        print(f"Error in fetching the data from the table: {e}")
        return None


def supabase_product_page_approval(necklace_id, nto_images_urls, cto_images_urls, mto_images_urls, video_urls,
                                   model_name):
    try:
        existing_record = supabase.table("MagicMirror").select("*").eq("Id", necklace_id).execute()

        def append_urls(existing_urls, new_urls):
            existing = convert_to_string(existing_urls) if existing_urls else ""
            new = convert_to_string(new_urls) if new_urls else ""

            if existing and new:
                return f"{existing},{new}"
            return new or existing

        if existing_record.data:
            record = existing_record.data[0]

            update_data = {
                "nto_images_urls": append_urls(record.get("nto_images_urls"), nto_images_urls),
                "cto_images_urls": append_urls(record.get("cto_images_urls"), cto_images_urls),
                "mto_images_urls": append_urls(record.get("mto_images_urls"), mto_images_urls),
                "video_urls": append_urls(record.get("video_urls"), video_urls)
            }
        else:
            update_data = {
                "nto_images_urls": convert_to_string(nto_images_urls),
                "cto_images_urls": convert_to_string(cto_images_urls),
                "mto_images_urls": convert_to_string(mto_images_urls),
                "video_urls": convert_to_string(video_urls)
            }

        result = supabase.table("MagicMirror") \
            .update(update_data) \
            .eq("Id", necklace_id) \
            .execute()

        res = supabase_jmproductpage_approval_flag(necklace_id, model_name, True)

        print(f"Successfully updated URLs for necklace ID: {necklace_id}")
        return result.data

    except Exception as e:
        error_msg = f"Error updating URLs in MagicMirror table: {str(e)}"
        print(error_msg)
        raise Exception(error_msg)


def convert_to_string(value):
    if value is None:
        return ""

    if isinstance(value, list):
        return ",".join(str(item) for item in value if item)

    if isinstance(value, str):
        # If it's already a string, clean it up
        items = [item.strip() for item in value.split(",") if item.strip()]
        return ",".join(items)

    return str(value)


def supabase_fetch_not_approved_necklaces():
    try:
        response = supabase.table("JM_Productpage").select("*").execute()

        unapproved_necklaces = []
        for record in response.data:
            approve_dict = record.get('approve', {})
            if not isinstance(approve_dict, dict):
                continue

            false_approvals = {
                model: status
                for model, status in approve_dict.items()
                if status is False
            }

            if false_approvals:
                filtered_record = {
                    'Id': record['Id'],
                    'approve': false_approvals
                }
                unapproved_necklaces.append(filtered_record)
        print(f"Unapproved necklaces: {unapproved_necklaces}")
        return unapproved_necklaces

    except Exception as e:
        print(f"Error fetching not approved necklaces: {str(e)}")
        raise e


def supabase_jmproductpage_approval_flag(necklace_id, model_name, flag_value):
    try:
        current_data = supabase.table("JM_Productpage").select("approve").eq("Id", necklace_id).execute()

        current_approve = current_data.data[0].get("approve", {}) if current_data.data else {}

        if model_name in current_approve and current_approve[model_name] is True:
            print(f"Skipping update for {model_name} as it's already approved")
            return current_data.data

        if isinstance(current_approve, dict):
            current_approve[model_name] = flag_value
        else:
            current_approve = {model_name: flag_value}

        response = supabase.table("JM_Productpage").update(
            {"approve": current_approve}
        ).eq("Id", necklace_id).execute()

        return response.data

    except Exception as e:
        print(f"Error updating approval flag for necklace ID {necklace_id}: {str(e)}")
        raise e


def upload_productpage_logs(necklace_id: str, status: bool, model_name: str) -> dict:
    try:
        existing_record = supabase.table("JM_productpagelog").select("*").eq("id", necklace_id).execute()

        if not existing_record.data:
            response = supabase.table("JM_productpagelog").insert([{
                "id": necklace_id,
                "status": bool(status),
                "model_name": model_name
            }]).execute()
            print("Inserted new record")
            return {
                "status": "success",
                "message": "Record inserted successfully"
            }
        else:
            existing_models = existing_record.data[0].get('model_name', '').split(',')
            existing_models = [model.strip() for model in existing_models]

            if model_name in existing_models:
                return {
                    "status": "error",
                    "message": f"Record already exists with model {model_name} and {necklace_id}"
                }
            else:
                updated_models = ','.join(existing_models + [model_name])

                response = supabase.table("JM_productpagelog").update({
                    "model_name": updated_models
                }).eq("id", necklace_id).execute()

                print(f"Updated record with new model {model_name}")
                return {
                    "status": "success",
                    "message": f"Added new model {model_name} to existing record"
                }

    except Exception as e:
        error_msg = f"Error updating product page log for necklace {necklace_id}: {str(e)}"
        logging.error(error_msg)
        raise Exception(f"Failed to update product page log: {str(e)}")


def fetch_productpage_logs(necklace_id):
    try:
        response = supabase.table("JM_productpagelog").select("*").eq("id", necklace_id).execute()
        for item in response.data:
            return item['status']
    except Exception as e:
        print(f"Error in fetching the data from the table", {e})
        return None


if __name__ == "__main__":
    # store_name = "ChamundiJewelsMandir"
    # necklaces = fetch_necklace_offset_each_store(store_name)
    #
    # count = 0
    # for necklace in necklaces:
    #     print(f"Necklace ID: {necklace.necklace_id}")
    #     print(f"URL: {necklace.necklace_url}")
    #     print(f"Lean offsets (x, y): ({necklace.x_lean_offset}, {necklace.y_lean_offset})")
    #     print(f"Broad offsets (x, y): ({necklace.x_broad_offset}, {necklace.y_broad_offset})")
    #     print(f"Category: {necklace.category}")
    #     print("-" * 50)
    #     count += 1
    #
    # print(f"Total necklaces fetched: {count}")
    # image_url = "https://lvuhhlrkcuexzqtsbqyu.supabase.co/storage/v1/object/public/JewelmirrorModelImages/M0X9e22b54f110493113feb79e3J.png"
    # url,type=fetch_model_body_type(image_url)
    # print(url,type)
    # ---------------------------------
    #
    # upload_information_to_new_table(necklace_id="CJM0025",
    #                                 nto_images_urls="https://lvuhhlrkcuexzqtsbqyu.supabase.co/storage/v1/object/public/ProductPageOutputs/CJM0025-M0X0f16ad748dd979a48d3f79b4J-nto-Gold_Necklaces.png",
    #                                 cto_images_urls="https://lvuhhlrkcuexzqtsbqyu.supabase.co/storage/v1/object/public/ProductPageOutputs/CJM0025-cto-Blue Lehenga.png,https://lvuhhlrkcuexzqtsbqyu.supabase.co/storage/v1/object/public/ProductPageOutputs/CJM0025-cto-Blue Kurti.png",
    #                                 mto_urls="https://lvuhhlrkcuexzqtsbqyu.supabase.co/storage/v1/object/public/ProductPageOutputs/CJM0025-M0X0f16ad748dd979a48d3f79b4J-mto-Blue_Kurti-lip_Carmine_Red_eye_Black_shadow_Maroon.png",
    #                                 video_urls="https://lvuhhlrkcuexzqtsbqyu.supabase.co/storage/v1/object/public/JewelmirrorVideoGeneration/video_bd48d59bc8f358c3.mp4")
    # --------------------------------
    # nto, cto, mto, video = supabase_image_fetch_product_page("CJM0025")
    #
    # print("nto", nto)
    # print("cto", cto)
    # print("mto", mto)
    # print("video", video)
    # # --------------------------------
    # res = supabase_product_page_approval(necklace_id="CJM0025",
    #                                      nto_images_urls="https://lvuhhlrkcuexzqtsbqyu.supabase.co/storage/v1/object/public/ProductPageOutputs/CJM0025-M0X0f16ad748dd979a48d3f79b4J-nto-Gold_Necklaces.png",
    #                                      cto_images_urls="https://lvuhhlrkcuexzqtsbqyu.supabase.co/storage/v1/object/public/ProductPageOutputs/CJM0025-cto-Blue Lehenga.png,https://lvuhhlrkcuexzqtsbqyu.supabase.co/storage/v1/object/public/ProductPageOutputs/CJM0025-cto-Blue Kurti.png",
    #                                      mto_images_urls="https://lvuhhlrkcuexzqtsbqyu.supabase.co/storage/v1/object/public/ProductPageOutputs/CJM0025-M0X0f16ad748dd979a48d3f79b4J-mto-Blue_Kurti-lip_Carmine_Red_eye_Black_shadow_Maroon.png",
    #                                      video_urls="https://lvuhhlrkcuexzqtsbqyu.supabase.co/storage/v1/object/public/JewelmirrorVideoGeneration/video_bd48d59bc8f358c3.mp4")
    # print(res)

    # res = supabase_fetch_not_approved_necklaces()
    # print(res)
    # --------------------------------
    # res = supabase_jmproductpage_approval_flag("CJM001")
    # print(res)

    response = upload_productpage_logs("CJM0025", "True")

    # -------------------------------
    response = fetch_productpage_logs("CJM0025")
    print(response)