from manim import *
import numpy as np

class OSKVSacredGeometry(Scene):
    def construct(self):
        self.camera.background_color = "#050510"
        
        # === PART 1: Seed of Life Formation ===
        # Start with a single circle
        center_circle = Circle(radius=1, color=BLUE_C, stroke_width=2)
        center_circle.set_stroke(opacity=0.8)
        
        self.play(Create(center_circle), run_time=1)
        
        # Add 6 circles around it (Seed of Life pattern)
        seed_circles = VGroup()
        for i in range(6):
            angle = i * PI / 3
            circle = Circle(radius=1, color=BLUE_C, stroke_width=2)
            circle.set_stroke(opacity=0.8)
            circle.move_to([np.cos(angle), np.sin(angle), 0])
            seed_circles.add(circle)
        
        self.play(
            LaggedStart(*[Create(c) for c in seed_circles], lag_ratio=0.15),
            run_time=1.5
        )
        
        seed_of_life = VGroup(center_circle, seed_circles)
        
        # === PART 2: Flower of Life Expansion ===
        # Add outer ring of circles
        outer_circles = VGroup()
        outer_positions = [
            (2, 0), (-2, 0),
            (1, np.sqrt(3)), (-1, np.sqrt(3)),
            (1, -np.sqrt(3)), (-1, -np.sqrt(3)),
            (1.5, np.sqrt(3)/2), (-1.5, np.sqrt(3)/2),
            (1.5, -np.sqrt(3)/2), (-1.5, -np.sqrt(3)/2),
            (0, np.sqrt(3)), (0, -np.sqrt(3)),
        ]
        
        for pos in outer_positions:
            circle = Circle(radius=1, color=PURPLE_B, stroke_width=1.5)
            circle.set_stroke(opacity=0.6)
            circle.move_to([pos[0], pos[1], 0])
            outer_circles.add(circle)
        
        self.play(
            LaggedStart(*[Create(c) for c in outer_circles], lag_ratio=0.08),
            run_time=1.5
        )
        
        flower_of_life = VGroup(seed_of_life, outer_circles)
        
        # Pulse the whole flower
        self.play(
            flower_of_life.animate.scale(1.1).set_stroke(opacity=1),
            rate_func=there_and_back,
            run_time=0.8
        )
        
        # === PART 3: Metatron's Cube Emerge ===
        # Fade flower slightly
        self.play(flower_of_life.animate.set_stroke(opacity=0.3), run_time=0.5)
        
        # Create the 13 circles of Metatron's Cube
        metatron_circles = VGroup()
        
        # Center
        mc_center = Circle(radius=0.15, color=TEAL, fill_opacity=0.8)
        metatron_circles.add(mc_center)
        
        # Inner hexagon (6 points)
        for i in range(6):
            angle = i * PI / 3
            circle = Circle(radius=0.15, color=TEAL, fill_opacity=0.8)
            circle.move_to([1.2 * np.cos(angle), 1.2 * np.sin(angle), 0])
            metatron_circles.add(circle)
        
        # Outer hexagon (6 points)
        for i in range(6):
            angle = i * PI / 3 + PI / 6
            circle = Circle(radius=0.15, color=TEAL, fill_opacity=0.8)
            circle.move_to([2.1 * np.cos(angle), 2.1 * np.sin(angle), 0])
            metatron_circles.add(circle)
        
        self.play(
            LaggedStart(*[GrowFromCenter(c) for c in metatron_circles], lag_ratio=0.05),
            run_time=1
        )
        
        # Connect all points (Metatron's Cube lines)
        metatron_lines = VGroup()
        points = [c.get_center() for c in metatron_circles]
        
        for i, p1 in enumerate(points):
            for j, p2 in enumerate(points):
                if i < j:
                    line = Line(p1, p2, stroke_width=1, color=WHITE)
                    line.set_stroke(opacity=0.4)
                    metatron_lines.add(line)
        
        self.play(
            LaggedStart(*[Create(l) for l in metatron_lines], lag_ratio=0.01),
            run_time=1.5
        )
        
        metatron = VGroup(metatron_circles, metatron_lines)
        
        # Rotate Metatron's Cube
        self.play(
            Rotate(metatron, PI / 6, about_point=ORIGIN),
            run_time=1.5
        )
        
        # === PART 4: Golden Spiral ===
        # Fade previous elements
        self.play(
            FadeOut(flower_of_life),
            metatron.animate.set_opacity(0.2),
            run_time=0.8
        )
        
        # Create golden spiral
        golden_ratio = (1 + np.sqrt(5)) / 2
        
        # Fibonacci squares
        fib_squares = VGroup()
        sizes = [0.1, 0.1, 0.2, 0.3, 0.5, 0.8, 1.3]
        colors = [BLUE, PURPLE, TEAL, BLUE_B, PURPLE_B, TEAL_B, BLUE_C]
        
        positions = [
            (0, 0), (0.1, 0), (0, 0.1), (-0.2, 0), (0, -0.3), (0.5, 0), (0, 0.8)
        ]
        
        # Simplified: just draw expanding squares
        current_pos = ORIGIN
        for i, (size, color) in enumerate(zip(sizes, colors)):
            sq = Square(side_length=size * 2, color=color, stroke_width=2)
            sq.move_to(current_pos)
            fib_squares.add(sq)
        
        # Actually let's do a proper golden spiral
        spiral_points = []
        t_values = np.linspace(0, 4 * PI, 200)
        a = 0.1
        b = 0.15
        
        for t in t_values:
            r = a * np.exp(b * t)
            x = r * np.cos(t)
            y = r * np.sin(t)
            spiral_points.append([x, y, 0])
        
        golden_spiral = VMobject()
        golden_spiral.set_points_smoothly(spiral_points)
        golden_spiral.set_stroke(color=[GOLD, YELLOW, ORANGE], width=3)
        
        self.play(Create(golden_spiral), run_time=2)
        
        # === PART 5: Sri Yantra Inspired Pattern ===
        self.play(
            FadeOut(golden_spiral),
            FadeOut(metatron),
            run_time=0.5
        )
        
        # Concentric triangles (simplified Sri Yantra)
        triangles = VGroup()
        
        for i in range(5):
            # Upward triangle
            tri_up = RegularPolygon(n=3, radius=2.5 - i * 0.4, color=PURPLE)
            tri_up.set_stroke(width=2, opacity=0.8 - i * 0.1)
            triangles.add(tri_up)
            
            # Downward triangle
            tri_down = RegularPolygon(n=3, radius=2.3 - i * 0.4, color=BLUE)
            tri_down.rotate(PI)
            tri_down.set_stroke(width=2, opacity=0.8 - i * 0.1)
            triangles.add(tri_down)
        
        # Outer circle
        outer_ring = Circle(radius=2.8, color=WHITE, stroke_width=2)
        
        self.play(
            Create(outer_ring),
            run_time=0.5
        )
        
        self.play(
            LaggedStart(*[Create(t) for t in triangles], lag_ratio=0.1),
            run_time=2
        )
        
        # Central bindu (dot)
        bindu = Dot(point=ORIGIN, radius=0.1, color=GOLD)
        bindu.set_glow_factor(1.5)
        
        self.play(GrowFromCenter(bindu), run_time=0.5)
        
        yantra = VGroup(outer_ring, triangles, bindu)
        
        # Rotate and pulse
        self.play(
            Rotate(yantra, PI / 12),
            yantra.animate.scale(1.05),
            rate_func=there_and_back,
            run_time=1
        )
        
        # === PART 6: OSKV Reveal ===
        self.play(
            yantra.animate.scale(0.4).shift(UP * 2),
            run_time=1
        )
        
        # OSKV text with sacred geometry styling
        oskv = Text("OSKV", font_size=140, weight=BOLD)
        oskv.set_color_by_gradient(GOLD, PURPLE, TEAL)
        oskv.shift(DOWN * 0.5)
        
        # Decorative circles around text
        deco_circles = VGroup()
        for i in range(12):
            angle = i * PI / 6
            small_c = Circle(radius=0.08, color=GOLD, fill_opacity=0.6)
            small_c.move_to(oskv.get_center() + 2.5 * np.array([np.cos(angle), np.sin(angle), 0]))
            deco_circles.add(small_c)
        
        self.play(
            Write(oskv),
            run_time=1.5
        )
        
        self.play(
            LaggedStart(*[GrowFromCenter(c) for c in deco_circles], lag_ratio=0.05),
            run_time=0.8
        )
        
        # Subtitle
        subtitle = Text("Sacred Geometry", font_size=32, slant=ITALIC)
        subtitle.set_color(WHITE)
        subtitle.set_opacity(0.7)
        subtitle.next_to(oskv, DOWN, buff=0.4)
        
        self.play(FadeIn(subtitle, shift=UP * 0.2), run_time=0.6)
        
        # Final glow pulse
        everything = VGroup(yantra, oskv, deco_circles, subtitle)
        
        self.play(
            everything.animate.scale(1.05),
            rate_func=there_and_back,
            run_time=1
        )
        
        self.wait(1.5)
        
        # Fade out with particle dispersion
        self.play(
            FadeOut(everything),
            run_time=1
        )
