// ps1c1.jsx — Clase 1.1 «Panorama del sector FV y etapas de un proyecto»
// Después de solar-lib.jsx. Exporta SCENES_PS1C1.

// Inicio de cada frase de la narración dentro de su escena (lo escribe scripts/narracion/tiempos.py)
const TIEMPOS_PS1C1 = [[0.6, 3.78, 11.03, 13.94], [0.6, 4.89, 8.17, 11.98], [0.6, 2.74, 7.32, 12.6, 20.18], [0.6, 2.93, 11.15], [0.6, 2.67, 4.71, 10.57, 12.59, 15.52, 20.52, 22.44, 24.79], [0.6, 2.41, 7.47, 10.67], [0.6, 2.35, 4.72, 9.55, 12.59], [0.6, 11.43, 12.74]]; // auto
const fr = (i, k) => (TIEMPOS_PS1C1[i] || [])[k] ?? 0.6 + k * 3.5;

// 1 · Por qué crece: costo de los módulos y versatilidad
function S_Crece({ start, dur }) {
  const t = useTime(); const s = start;
  const bars = [100, 78, 60, 46, 36, 29, 24, 20, 17, 15, 14];
  const x0 = 170, y0 = 820, bw = 52, gap = 18, hMax = 430;
  const grow = segS(t, s + fr(1, 2), 2.4);
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="Por qué crece la fotovoltaica" title="La fuente de generación que más rápido crece" />
      <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
        <g opacity={segS(t, s + fr(1, 1), 0.6)}>
          <text x={x0} y={y0 - hMax - 92} fill={SL.ink} fontFamily={DISP} fontSize="26" fontWeight="700">Precio relativo de los módulos</text>
          <text x={x0} y={y0 - hMax - 60} fill={SL.dim} fontFamily={MONO} fontSize="16">índice ilustrativo · inicio de la década = 100</text>
          <line x1={x0 - 10} y1={y0} x2={x0 + bars.length * (bw + gap)} y2={y0} stroke={SL.lineS} strokeWidth="2" />
          {bars.map((v, i) => {
            const k = clamp(grow * bars.length - i, 0, 1);
            const h = (v / 100) * hMax * k;
            return (
              <g key={i}>
                <rect x={x0 + i * (bw + gap)} y={y0 - h} width={bw} height={h} rx="4" fill={i === bars.length - 1 ? SL.sun : 'rgba(251,191,36,0.35)'} />
                {k >= 1 && (i === 0 || i === bars.length - 1) && <text x={x0 + i * (bw + gap) + bw / 2} y={y0 - h - 12} textAnchor="middle" fill={SL.ink} fontFamily={MONO} fontSize="20" fontWeight="600">{v}</text>}
              </g>
            );
          })}
          <text x={x0} y={y0 + 34} fill={SL.mut} fontFamily={MONO} fontSize="16">año 0</text>
          <text x={x0 + (bars.length - 1) * (bw + gap)} y={y0 + 34} fill={SL.mut} fontFamily={MONO} fontSize="16">año 10</text>
          {grow >= 1 && <text x={x0 + 300} y={y0 - 300} fill={SL.sun} fontFamily={DISP} fontSize="40" fontWeight="700">−80 % o más</text>}
        </g>
      </svg>
      {[
        { title: 'Sin piezas móviles', body: 'Poco desgaste y mantenimiento sencillo.' },
        { title: 'Modular', body: 'Se instala y crece por bloques.' },
        { title: 'Va donde hay espacio', body: 'Techo, estacionamiento o terreno.' },
      ].map((c, i) => (
        <CardS key={i} at={s + fr(1, 3) + i * 1.2} x={1060} y={300 + i * 190} w={700} accent={SL.sun} title={c.title}>{c.body}</CardS>
      ))}
    </SceneS>
  );
}

// 2 · Escalas: residencial, comercial-industrial, gran escala
function S_Escalas({ start, dur }) {
  const t = useTime(); const s = start;
  const cols = [
    { k: 'Residencial', p: 'Pocos kW', d: 'Techo de una casa · baja tensión', at: fr(2, 1), draw: (c) => (
      <g><path d="M-90 40 V-10 L0 -70 L90 -10 V40 Z" fill="none" stroke={c} strokeWidth="4" strokeLinejoin="round" /><path d="M-60 -20 L-5 -58 L40 -28 L-15 10 Z" fill="#0d2a52" stroke={SL.ca} strokeWidth="3" /></g>) },
    { k: 'Comercial e industrial', p: 'Decenas a cientos de kW', d: 'Naves y estacionamientos · BT o MT', at: fr(2, 2), draw: (c) => (
      <g><path d="M-120 40 V-20 L-60 -50 V-20 L0 -50 V-20 L60 -50 V-20 L120 -50 V40 Z" fill="none" stroke={c} strokeWidth="4" strokeLinejoin="round" />{[-100, -40, 20, 80].map(x => <path key={x} d={`M${x} -24 L${x + 10} -44 H${x + 44} L${x + 34} -24 Z`} fill="#0d2a52" stroke={SL.ca} strokeWidth="2.5" />)}</g>) },
    { k: 'Gran escala', p: 'Megavatios', d: 'Suelo · media o alta tensión', at: fr(2, 3), draw: (c) => (
      <g>{[0, 1, 2].map(r => [0, 1, 2, 3].map(q => <path key={r + '-' + q} d={`M${-130 + q * 66 + r * 14} ${-40 + r * 32} l12 -22 h44 l-12 22 z`} fill="#0d2a52" stroke={SL.ca} strokeWidth="2.5" />))}<line x1="-140" y1="44" x2="140" y2="44" stroke={c} strokeWidth="4" /></g>) },
  ];
  const focus = segS(t, s + fr(2, 4), 0.8);
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="Escalas de planta" title="No todas las plantas son iguales" />
      {cols.map((c, i) => {
        const p = popS(t, s + c.at);
        const isCI = i === 1;
        const dimOut = 1 - (isCI ? 0 : 0.55 * focus);
        return (
          <div key={i} style={{ position: 'absolute', left: 150 + i * 560, top: 330, width: 500, height: 520, opacity: p.op * dimOut, transform: `translateY(${p.ty}px) scale(${isCI ? 1 + 0.04 * focus : p.sc})`, borderRadius: 16, background: SL.paper, border: `${isCI && focus > 0.5 ? 2 : 1}px solid ${isCI && focus > 0.5 ? SL.sun : SL.lineS}`, boxShadow: SL.shadow, padding: 30, display: 'flex', flexDirection: 'column', gap: 12 }}>
            <svg viewBox="-160 -90 320 150" style={{ width: '100%', height: 200 }}>{c.draw(SL.ink)}</svg>
            <div style={{ fontFamily: DISP, fontSize: 32, fontWeight: 700, color: SL.ink }}>{c.k}</div>
            <div style={{ fontFamily: MONO, fontSize: 24, fontWeight: 600, color: SL.sun }}>{c.p}</div>
            <div style={{ fontFamily: DISP, fontSize: 21, color: SL.mut, lineHeight: 1.4 }}>{c.d}</div>
            {isCI && focus > 0.05 && <div style={{ marginTop: 'auto', opacity: focus, fontFamily: MONO, fontSize: 15, letterSpacing: '0.18em', color: '#1a1205', background: SL.sun, alignSelf: 'flex-start', padding: '6px 12px', borderRadius: 6, fontWeight: 700 }}>CASO DEL CURSO</div>}
          </div>
        );
      })}
    </SceneS>
  );
}

// 3 · Quién participa
function S_Actores({ start, dur }) {
  const t = useTime(); const s = start;
  const cx = 960, cy = 580, R = 300;
  const actors = ['Cliente', 'Diseñador', 'Instalador', 'Distribuidora', 'Operación y mantenimiento'];
  const hub = popS(t, s + fr(3, 0));
  const you = segS(t, s + fr(3, 2), 0.8);
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="Quién participa" title="Un proyecto, muchas manos" />
      <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
        {actors.map((a, i) => {
          const ang = -Math.PI / 2 + (i * 2 * Math.PI) / actors.length;
          const x = cx + Math.cos(ang) * R * 1.35, y = cy + Math.sin(ang) * R * 0.78;
          const at = s + fr(3, 1) + i * 0.9;
          const k = segS(t, at, 0.6);
          return (
            <g key={a} opacity={k}>
              <line x1={cx} y1={cy} x2={cx + (x - cx) * k} y2={cy + (y - cy) * k} stroke={you > 0 ? SL.sun : SL.lineS} strokeWidth={2 + 2 * you} strokeDasharray="6 8" />
              <rect x={x - 150} y={y - 38} width="300" height="76" rx="38" fill={SL.paper} stroke={SL.lineS} strokeWidth="2" />
              <text x={x} y={y + 9} textAnchor="middle" fill={SL.ink} fontFamily={DISP} fontSize={a.length > 16 ? 21 : 26} fontWeight="600">{a}</text>
            </g>
          );
        })}
        <g opacity={hub.op}>
          <circle cx={cx} cy={cy} r={95} fill={SL.paper} stroke={SL.sun} strokeWidth="3" />
          <text x={cx} y={cy - 6} textAnchor="middle" fill={SL.sun} fontFamily={DISP} fontSize="30" fontWeight="700">Proyecto</text>
          <text x={cx} y={cy + 28} textAnchor="middle" fill={SL.ink} fontFamily={DISP} fontSize="24">fotovoltaico</text>
        </g>
      </svg>
      <CapS start={s + fr(3, 2)} dur={dur - fr(3, 2) - 0.3} size={30} weight={500} color={SL.mut} y="93%">Como técnico, tu trabajo toca a casi todos.</CapS>
    </SceneS>
  );
}

// 4 · Etapas del proyecto (línea de tiempo)
function S_Etapas({ start, dur }) {
  const t = useTime(); const s = start;
  const steps = [
    { k: 'Evaluación', d: 'Consumo, recurso solar, visita al sitio', at: fr(4, 1) },
    { k: 'Diseño', d: 'Equipos, strings, protecciones, planos', at: fr(4, 3) },
    { k: 'Permisos', d: 'Licencias y solicitud de conexión', at: fr(4, 5) },
    { k: 'Montaje', d: 'Obra, estructura, cableado', at: fr(4, 6) },
    { k: 'Puesta en marcha', d: 'Pruebas y energización', at: fr(4, 7) },
    { k: 'Operación', d: 'Mantenimiento · 25 años o más', at: fr(4, 8) },
  ];
  const x0 = 170, x1 = 1750, y = 560;
  const w = (x1 - x0) / (steps.length - 1);
  const lineK = segS(t, s + fr(4, 1), (fr(4, 8) - fr(4, 1)) + 0.6);
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="Etapas de un proyecto" title="De la idea a la operación" />
      <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
        <line x1={x0} y1={y} x2={x1} y2={y} stroke={SL.lineS} strokeWidth="4" />
        <line x1={x0} y1={y} x2={x0 + (x1 - x0) * lineK} y2={y} stroke={SL.sun} strokeWidth="4" />
        {steps.map((st, i) => {
          const p = popS(t, s + st.at, 0.5, 0);
          const x = x0 + i * w;
          return (
            <g key={i} opacity={p.op}>
              <circle cx={x} cy={y} r="26" fill={SL.paper} stroke={SL.sun} strokeWidth="4" />
              <text x={x} y={y + 8} textAnchor="middle" fill={SL.sun} fontFamily={MONO} fontSize="22" fontWeight="700">{i + 1}</text>
            </g>
          );
        })}
      </svg>
      {steps.map((st, i) => {
        const p = popS(t, s + st.at, 0.5, 14);
        const up = i % 2 === 0;
        return (
          <div key={i} style={{ position: 'absolute', left: x0 + i * w - 140, top: up ? 330 : 620, width: 280, textAlign: 'center', opacity: p.op, transform: `translateY(${p.ty}px)` }}>
            <div style={{ fontFamily: DISP, fontSize: 30, fontWeight: 700, color: SL.ink }}>{st.k}</div>
            <div style={{ fontFamily: DISP, fontSize: 20, color: SL.mut, lineHeight: 1.4, marginTop: 8 }}>{st.d}</div>
          </div>
        );
      })}
    </SceneS>
  );
}

// 5 · El error se arrastra; el proyecto integrador se arma módulo a módulo
function S_Integrador({ start, dur }) {
  const t = useTime(); const s = start;
  const chain = ['Visita al sitio', 'Diseño', 'Montaje', 'Producción'];
  const err = segS(t, s + fr(5, 1), 2.4);
  const fill = segS(t, s + fr(5, 3), 3.5);
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="El proyecto integrador" title="Cada etapa depende de la anterior" />
      <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
        {chain.map((c, i) => {
          const x = 230 + i * 300, y = 400;
          const on = err * chain.length > i;
          return (
            <g key={c} opacity={popS(t, s + fr(5, 0) + i * 0.3).op}>
              {i > 0 && <path d={`M${x - 85} ${y} H${x - 40}`} stroke={on ? SL.bad : SL.lineS} strokeWidth="4" markerEnd="" />}
              <rect x={x - 40} y={y - 44} width="240" height="88" rx="12" fill={SL.paper} stroke={on ? SL.bad : SL.lineS} strokeWidth="2.5" />
              <text x={x + 80} y={y + 9} textAnchor="middle" fill={SL.ink} fontFamily={DISP} fontSize="24" fontWeight="600">{c}</text>
            </g>
          );
        })}
        {err > 0.02 && <text x={230} y={500} fill={SL.bad} fontFamily={DISP} fontSize="24" opacity={err}>Una sombra que no se vio → menos energía durante años</text>}
      </svg>
      <div style={{ position: 'absolute', left: 230, top: 600, opacity: popS(t, s + fr(5, 2)).op }}>
        <div style={{ fontFamily: MONO, fontSize: 17, letterSpacing: '0.2em', color: SL.sun, fontWeight: 600, marginBottom: 18 }}>EXPEDIENTE TÉCNICO · UNA PIEZA POR MÓDULO</div>
        <div style={{ display: 'flex', gap: 14 }}>
          {[...Array(10)].map((_, i) => {
            const on = fill * 10 > i;
            return (
              <div key={i} style={{ width: 132, height: 150, borderRadius: 10, border: `2px solid ${i === 0 ? SL.sun : on ? 'rgba(251,191,36,0.6)' : SL.lineS}`, background: i === 0 ? 'rgba(251,191,36,0.14)' : on ? 'rgba(251,191,36,0.06)' : SL.paper, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 6 }}>
                <span style={{ fontFamily: MONO, fontSize: 26, fontWeight: 700, color: on ? SL.sun : SL.dim }}>M{i + 1}</span>
                {i === 0 && <span style={{ fontFamily: DISP, fontSize: 15, color: SL.ink, textAlign: 'center', lineHeight: 1.2 }}>Definición<br />del proyecto</span>}
              </div>
            );
          })}
        </div>
      </div>
    </SceneS>
  );
}

// 6 · Alcance y seguridad
function S_Alcance({ start, dur }) {
  const t = useTime(); const s = start;
  const glow = 0.5 + 0.5 * Math.sin(t * 3);
  const shine = segS(t, s + fr(6, 3), 0.8);
  return (
    <SceneS start={start} dur={dur}>
      <HeadS s={s} dur={dur} kicker="Alcance del curso" title="Formativo, pero con criterio de obra" />
      <CardS at={s + fr(6, 1)} x={170} y={320} w={760} accent={SL.ca} no="CÁLCULOS FORMATIVOS" title="Un proyecto real necesita más">
        Ingeniería aprobada por profesionales autorizados y la norma vigente de tu país, que cambia de un lugar a otro.
      </CardS>
      <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
        <g opacity={shine}>
          <SunG x={1500} y={330} r={48} t={t} />
          <PanelG x={1240} y={430} w={150} h={240} glow={shine * glow} />
          <path d="M1315 670 V760 H1560" fill="none" stroke={SL.cc} strokeWidth="5" strokeDasharray="14 10" strokeDashoffset={-(t * 50) % 24} />
          <rect x="1560" y="720" width="160" height="80" rx="10" fill={SL.paper} stroke={SL.bad} strokeWidth="3" />
          <text x="1640" y="758" textAnchor="middle" fill={SL.bad} fontFamily={MONO} fontSize="20" fontWeight="700">OFF</text>
          <text x="1640" y="784" textAnchor="middle" fill={SL.mut} fontFamily={DISP} fontSize="15">inversor apagado</text>
          <DcG x={1330} y={810} w={36} /><text x="1360" y="818" fill={SL.cc} fontFamily={MONO} fontSize="22" fontWeight="700">sigue con tensión</text>
        </g>
      </svg>
      <SafetyS at={s + fr(6, 3)} y="86%">Un módulo al sol siempre genera tensión: no se apaga como un interruptor.</SafetyS>
    </SceneS>
  );
}

const SCENES_PS1C1 = [
  { C: (p) => <TitleCardS {...p} moduleNo={1} claseNo={1} title="Panorama del sector fotovoltaico" objetivo="Por qué crece la energía solar, qué escalas de planta existen y por qué etapas pasa un proyecto, de la idea a la operación." />, dur: 21, label: 'Apertura' },
  { C: S_Crece, dur: 19, label: 'Por qué crece' },
  { C: S_Escalas, dur: 25, label: 'Escalas' },
  { C: S_Actores, dur: 15, label: 'Quién participa' },
  { C: S_Etapas, dur: 32, label: 'Etapas del proyecto' },
  { C: S_Integrador, dur: 16, label: 'El proyecto integrador' },
  { C: S_Alcance, dur: 16, label: 'Alcance y seguridad' },
  { C: (p) => <ClosingS {...p} line="La fotovoltaica crece por costo y versatilidad. Todo proyecto va de la evaluación a la operación." question="¿En qué etapa detectarías una sombra que puede arruinar la producción?" />, dur: 17, label: 'Cierre' },
];
window.SCENES_PS1C1 = SCENES_PS1C1;
