from datetime import date
from pathlib import Path
from typing import Any

from sqlalchemy.orm import Session

from app import models


SEED_DIR = Path(__file__).resolve().parents[1] / "seed"
SAMPLE_TRANSCRIPT_PATH = SEED_DIR / "sample_transcript_001.txt"


def read_sample_transcript() -> str:
    return SAMPLE_TRANSCRIPT_PATH.read_text(encoding="utf-8")


def create_mock_session(db: Session) -> models.FeishuSession:
    index = db.query(models.FeishuSession).count() + 1
    session = models.FeishuSession(
        session_code=f"MOCK-{index:04d}",
        title="大本营周三答疑 mock 场次",
        date=date.today(),
        teachers="院长, 辅导老师",
        feishu_doc_url="mock://feishu/sample_transcript_001",
        feishu_record_id=f"mock-record-{index:04d}",
        source_file_name="sample_transcript_001.txt",
        source_text=read_sample_transcript(),
        process_status=models.ProcessStatus.unprocessed,
    )
    db.add(session)
    db.commit()
    db.refresh(session)
    return session


def mock_ai_cleaning_result(session_id: int) -> list[dict[str, Any]]:
    return [
        {
            "session_id": str(session_id),
            "qa_id": "mock-qa-001",
            "raw_question": "我和孩子沟通时总是忍不住发火，孩子现在一听我说话就躲开，我该怎么办？",
            "normalized_question": "和孩子沟通时容易发火，孩子开始回避，应该如何调整？",
            "raw_answer": "先不要急着讲道理，先看见自己发火背后的着急和无力。可以把要求先放低一点，用一句具体的话表达当下感受，比如我现在有点急，我想先停一下。",
            "normalized_answer": "先觉察自己发火背后的着急和无力，暂停说教，把要求放低，用具体语言表达当下感受。",
            "suggested_standard_question": "亲子沟通中总忍不住发火，如何先做自我调整？",
            "suggested_standard_answer": "可以先暂停说教，觉察自己发火背后的着急和无力，再用具体、低压力的语言表达当下感受。这个回答不能替代个体咨询，也不承诺亲子关系会立即改善。",
            "answer_person": "辅导老师",
            "primary_topic": "亲子关系",
            "problem_tags": ["亲子沟通", "情绪觉察", "发火"],
            "course_stage": "觉知",
            "audience_tags": ["家长", "亲子关系困扰者"],
            "emotion_intensity": "medium",
            "risk_level": "medium",
            "risk_types": ["未成年人信息", "家庭矛盾"],
            "risk_notes": "涉及孩子和家庭互动，需要人工确认是否包含可识别隐私。",
            "need_desensitization": True,
            "desensitization_status": "needed",
            "suggested_review_status": "pending",
            "suggested_to_standard_qa": True,
            "source_timestamp": "00:03:12",
            "review_notes": "建议审核时确认是否需要进一步脱敏孩子细节。",
        },
        {
            "session_id": str(session_id),
            "qa_id": "mock-qa-002",
            "raw_question": "我学习大本营后知道自己总是拖延，怎么开始一个小行动？",
            "normalized_question": "意识到自己经常拖延后，如何启动一个小行动？",
            "raw_answer": "不要一开始就设很大的目标。先选一个今天能完成的动作，比如写三句话、整理十分钟。关键是让身体先体验到我可以开始。",
            "normalized_answer": "先设置当天能完成的小动作，让身体体验到可以开始，而不是直接设很大目标。",
            "suggested_standard_question": "面对拖延时，如何启动一个足够小的行动？",
            "suggested_standard_answer": "可以先选择一个今天就能完成的小动作，例如写三句话或整理十分钟。重点不是追求完美，而是让自己先体验到可以开始。",
            "answer_person": "院长",
            "primary_topic": "行动力与拖延",
            "problem_tags": ["拖延", "小行动", "自我启动"],
            "course_stage": "大本营综合",
            "audience_tags": ["大本营学员"],
            "emotion_intensity": "low",
            "risk_level": "low",
            "risk_types": [],
            "risk_notes": "未发现明显隐私或边界风险。",
            "need_desensitization": False,
            "desensitization_status": "not_needed",
            "suggested_review_status": "pending",
            "suggested_to_standard_qa": True,
            "source_timestamp": "00:16:44",
            "review_notes": "可作为标准问答候选，但仍需人工审核。",
        },
        {
            "session_id": str(session_id),
            "qa_id": "mock-qa-003",
            "raw_question": "我朋友说自己有抑郁症，我能不能用课程里的方法帮她判断严重程度？",
            "normalized_question": "朋友提到抑郁症时，能否用课程方法判断严重程度？",
            "raw_answer": "这类情况不要做诊断，也不要替对方判断严重程度。你可以表达关心，建议对方寻求专业医生或心理专业人员帮助。",
            "normalized_answer": "不要做诊断或严重程度判断，可以表达关心，并建议对方寻求专业医生或心理专业人员帮助。",
            "suggested_standard_question": "遇到疑似心理健康问题时，课程学习者应该如何把握边界？",
            "suggested_standard_answer": "不做心理诊断，不判断严重程度；可以表达关心，并建议对方寻求专业医生或心理专业人员帮助。",
            "answer_person": "辅导老师",
            "primary_topic": "其他",
            "problem_tags": ["心理边界", "专业转介"],
            "course_stage": "不确定",
            "audience_tags": ["大本营学员"],
            "emotion_intensity": "high",
            "risk_level": "high",
            "risk_types": ["心理 / 医疗边界"],
            "risk_notes": "涉及心理诊断边界，必须人工复审，默认不可自动入库。",
            "need_desensitization": False,
            "desensitization_status": "not_needed",
            "suggested_review_status": "pending",
            "suggested_to_standard_qa": False,
            "source_timestamp": "00:27:05",
            "review_notes": "高风险边界题，不建议自动进入标准问答库。",
        },
    ]

