from rest_framework import serializers

from .models import (
    BaseAssetGroup,
    BgmTrack,
    ExportJob,
    Project,
    ProjectStage,
    ScriptSegment,
    ScriptVersion,
    StoryboardFrame,
    StoryboardVersion,
    SubtitleTrack,
    Timeline,
    TimelineClip,
    VideoSegment,
    VideoSegmentVersion,
)


class ProjectStageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProjectStage
        fields = ["id", "stage", "status", "started_at", "completed_at", "error_message", "metadata"]
        read_only_fields = fields


class VideoSegmentSerializer(serializers.ModelSerializer):
    adopted_asset = serializers.SerializerMethodField()

    class Meta:
        model = VideoSegment
        fields = ["id", "sort_order", "target_duration_seconds", "status", "error_message", "adopted_version", "adopted_asset"]
        read_only_fields = ["id", "sort_order", "target_duration_seconds", "status", "error_message", "adopted_version"]

    def get_adopted_asset(self, obj):
        # pipeline stage4 缩略图:暴露已采用版本对应的资产 id(供前端在 assets 里解析 preview_url)
        version = obj.adopted_version
        return str(version.asset_id) if version and version.asset_id else None


class BaseAssetGroupSerializer(serializers.ModelSerializer):
    candidate_assets = serializers.PrimaryKeyRelatedField(many=True, read_only=True)

    class Meta:
        model = BaseAssetGroup
        fields = ["id", "kind", "prompt", "adopted_asset", "candidate_assets", "version", "metadata", "created_at"]
        read_only_fields = fields


class StoryboardFrameSerializer(serializers.ModelSerializer):
    class Meta:
        model = StoryboardFrame
        fields = ["id", "script_segment", "asset", "sort_order", "prompt"]
        read_only_fields = fields


class StoryboardVersionSerializer(serializers.ModelSerializer):
    frames = StoryboardFrameSerializer(many=True, read_only=True)

    class Meta:
        model = StoryboardVersion
        fields = ["id", "prompt", "is_adopted", "frames", "created_at", "updated_at"]
        read_only_fields = fields


class VideoSegmentVersionSerializer(serializers.ModelSerializer):
    class Meta:
        model = VideoSegmentVersion
        fields = ["id", "video_segment", "asset", "prompt", "is_adopted", "metadata", "created_at"]
        read_only_fields = fields


class TimelineClipSerializer(serializers.ModelSerializer):
    class Meta:
        model = TimelineClip
        fields = ["id", "asset", "sort_order", "start_ms", "duration_ms", "trim_start_ms", "trim_end_ms"]
        read_only_fields = ["id"]


class TimelineExportJobSerializer(serializers.ModelSerializer):
    class Meta:
        model = ExportJob
        fields = ["id", "status", "output_asset", "progress", "error_message", "created_at", "updated_at"]
        read_only_fields = fields


class SubtitleTrackSerializer(serializers.ModelSerializer):
    class Meta:
        model = SubtitleTrack
        fields = ["id", "content", "style", "enabled"]
        read_only_fields = fields


class BgmTrackSerializer(serializers.ModelSerializer):
    class Meta:
        model = BgmTrack
        fields = ["id", "asset", "volume", "start_ms"]
        read_only_fields = fields


class TimelineSerializer(serializers.ModelSerializer):
    clips = TimelineClipSerializer(many=True, read_only=True)
    export_jobs = TimelineExportJobSerializer(many=True, read_only=True)
    subtitle_tracks = SubtitleTrackSerializer(many=True, read_only=True)
    bgm_tracks = BgmTrackSerializer(many=True, read_only=True)

    class Meta:
        model = Timeline
        fields = ["id", "name", "aspect_ratio", "resolution", "duration_seconds", "metadata", "clips", "export_jobs", "subtitle_tracks", "bgm_tracks"]
        read_only_fields = ["id", "clips", "export_jobs", "subtitle_tracks", "bgm_tracks"]


class ExportJobSerializer(serializers.ModelSerializer):
    class Meta:
        model = ExportJob
        fields = ["id", "status", "output_asset", "progress", "error_message", "metadata", "created_at", "updated_at"]
        read_only_fields = fields


class ScriptSegmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = ScriptSegment
        fields = ["id", "sort_order", "duration_seconds", "narration", "visual_prompt", "product_points"]
        read_only_fields = fields


class ScriptVersionSerializer(serializers.ModelSerializer):
    segments = ScriptSegmentSerializer(many=True, read_only=True)

    class Meta:
        model = ScriptVersion
        fields = ["id", "title", "content", "source", "is_adopted", "segments", "created_at", "updated_at"]
        read_only_fields = fields


class ProjectSerializer(serializers.ModelSerializer):
    stages = ProjectStageSerializer(many=True, read_only=True)
    video_segments = VideoSegmentSerializer(many=True, read_only=True)
    script_versions = ScriptVersionSerializer(many=True, read_only=True)
    base_asset_groups = BaseAssetGroupSerializer(many=True, read_only=True)
    storyboard_versions = StoryboardVersionSerializer(many=True, read_only=True)
    timeline = TimelineSerializer(read_only=True)

    class Meta:
        model = Project
        fields = [
            "id",
            "name",
            "product",
            "status",
            "current_stage",
            "budget_limit",
            "failure_reason",
            "metadata",
            "stages",
            "script_versions",
            "base_asset_groups",
            "storyboard_versions",
            "video_segments",
            "timeline",
            "created_at",
            "updated_at",
        ]
        read_only_fields = ["id", "status", "current_stage", "failure_reason", "created_at", "updated_at"]
