# backend/tests/_factories.py — 테스트용 최소 데이터 빌더 (phase-16+: 3개 도메인 시드 제거 후)
# 시드가 더 이상 메일/일정/작업 데이터를 깔지 않으므로, 기능 테스트는 필요한 데이터를 직접 만든다.
from app.models import (
    CalEvent,
    ConnectorAccount,
    ConnectorDomain,
    ConnectorMode,
    ConnState,
    Email,
    MailAccount,
    Meeting,
    MeetingAction,
    Prio,
    Task,
    TaskComment,
    TaskStatus,
)


def make_task(s, *, id, title="테스트 작업", project_id="me", parent_id=None,
              status="todo", assignee_id="jiwoo", prio="보통", **kw):
    t = Task(
        id=id, title=title, project_id=project_id, parent_id=parent_id,
        status=TaskStatus(status), assignee_id=assignee_id, prio=Prio(prio), **kw,
    )
    s.add(t)
    s.commit()
    s.refresh(t)
    return t


def make_comment(s, *, id, task_id, person_id="minseo", text="확인했습니다"):
    c = TaskComment(id=id, task_id=task_id, person_id=person_id, text=text)
    s.add(c)
    s.commit()
    return c


def make_mail_account(s, *, id="work", name="회사", email="jiwoo@gmail.com",
                      tone="blue", kind="Google", connector="gmail", sort_order=0):
    a = MailAccount(id=id, name=name, email=email, tone=tone, kind=kind,
                    connector=connector, sort_order=sort_order)
    s.add(a)
    s.commit()
    return a


def make_email(s, *, id, account="work", from_key="hyunwoo", to="나", subject="테스트 메일",
               time="방금", date="오늘", read=False, starred=False, has_attach=False,
               labels=None, preview="", body=None, ai_json=None, sort_order=0,
               received_at=None):
    e = Email(
        id=id, account=account, from_key=from_key, to=to, subject=subject, time=time,
        date=date, read=read, starred=starred, has_attach=has_attach,
        labels=labels or [], preview=preview, body=body or [], ai_json=ai_json or {},
        sort_order=sort_order, received_at=received_at,
    )
    s.add(e)
    s.commit()
    s.refresh(e)
    return e


def make_cal_event(s, *, id, day=8, start="10:00", end="11:00", title="테스트 일정",
                   cal="work", loc="", note="", soon=False, people="", sort_order=0):
    ev = CalEvent(id=id, day=day, start=start, end=end, title=title, cal=cal,
                  loc=loc, note=note, soon=soon, people=people, sort_order=sort_order)
    s.add(ev)
    s.commit()
    s.refresh(ev)
    return ev


def make_meeting(s, *, event_id, phase="upcoming", one_on_one=False):
    m = Meeting(id=event_id, event_id=event_id, phase=phase, one_on_one=one_on_one)
    s.add(m)
    s.commit()
    return m


def make_meeting_action(s, *, id, meeting_id=None, event_id=None, idx=0, who="나",
                        text="액션", when_text="이번 주", source="meeting", added=False):
    a = MeetingAction(id=id, meeting_id=meeting_id, event_id=event_id, idx=idx, who=who,
                      text=text, when_text=when_text, source=source, added=added)
    s.add(a)
    s.commit()
    return a


def make_connected_mail_account(s, *, id="ca-mail-gmail-test", provider="gmail",
                                email="jiwoo@gmail.com"):
    """발송 테스트용: real+connected 메일 ConnectorAccount."""
    a = ConnectorAccount(
        id=id, user_id="jiwoo", domain=ConnectorDomain.mail, provider=provider,
        mode=ConnectorMode.real, state=ConnState.connected,
        external_account_id=email, name=email, kind="Google",
    )
    s.add(a)
    s.commit()
    return a
