# -*- coding: utf-8 -*-
from .base_dwd_task import BaseDwdTask
from loaders.facts.ticket import TicketLoader
from utils.windowing import build_window_segments

class TicketDwdTask(BaseDwdTask):
    """
    DWD Task: Process Ticket Details from ODS to Fact Tables
    Source: billiards_ods.ods_ticket_detail
    Targets: 
        - billiards.fact_order
        - billiards.fact_order_goods
        - billiards.fact_table_usage
        - billiards.fact_assistant_service
    """

    def get_task_code(self) -> str:
        return "TICKET_DWD"

    def execute(self) -> dict:
        self.logger.info(f"Starting {self.get_task_code()} task")

        base_start, base_end, _ = self._get_time_window()
        segments = build_window_segments(
            self.config,
            base_start,
            base_end,
            tz=self.tz,
            override_only=True,
        )
        if not segments:
            segments = [(base_start, base_end)]

        total_segments = len(segments)
        if total_segments > 1:
            self.logger.info(f"{self.get_task_code()}: ????? {total_segments} ?")

        loader = TicketLoader(self.db, logger=self.logger)
        store_id = self.config.get("app.store_id")

        total_inserted = 0
        total_errors = 0

        for idx, (window_start, window_end) in enumerate(segments, start=1):
            self.logger.info(
                f"Processing window {idx}/{total_segments}: {window_start} to {window_end}"
            )
            batches = self.iter_ods_rows(
                table_name="billiards_ods.settlement_ticket_details",
                columns=["payload", "fetched_at", "source_file", "record_index"],
                start_time=window_start,
                end_time=window_end
            )

            for batch in batches:
                if not batch:
                    continue

                tickets = []
                for row in batch:
                    payload = self.parse_payload(row)
                    if payload:
                        tickets.append(payload)

                inserted, errors = loader.process_tickets(tickets, store_id)
                total_inserted += inserted
                total_errors += errors

            self.db.commit()

        overall_start = segments[0][0]
        overall_end = segments[-1][1]

        self.logger.info(
            f"Task {self.get_task_code()} completed. Inserted: {total_inserted}, Errors: {total_errors}"
        )

        return {
            "status": "success",
            "inserted": total_inserted,
            "errors": total_errors,
            "window_start": overall_start.isoformat(),
            "window_end": overall_end.isoformat()
        }

